谁能指出我如何使用Windows Phone 8将 JSON 发布到RESTful API(例如 Web API)的工作示例?我有一个 GET 的工作示例,但似乎找不到任何 POST 的工作示例。我为 C# 找到的所有 POST 示例都不适用于 Windows Phone 8(由于精简的 .NET 框架)。
问问题
5118 次
4 回答
3
好的,我终于能够提出一个可行的解决方案,所以我想将其发布回来以确保完整性。但是,如果有人知道在 Windows Phone 8 中执行此操作的更好方法,我很乐意看到它!
public void SendPost(Uri uri, string json)
{
var webClient = new WebClient();
webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
webClient.UploadStringCompleted += this.sendPostCompleted;
webClient.UploadStringAsync(uri, "POST", json);
}
private void sendPostCompleted(object sender, UploadStringCompletedEventArgs e)
{
// Handle result
Console.WriteLine("HTTP POST Result: {0}", e.Result);
}
于 2013-05-22T15:56:21.033 回答
0
最好的方法是使用 Restsharp:
昏暗客户端作为新的 RestSharp.RestClient("https://stuff.com/api/") Dim req As New RestSharp.RestRequest("dothings", Method.POST) req.RequestFormat = DataFormat.Json req.AddBody(使用 {.param1 = "stuff1", .param2="stuff2"} 的新 reqAuth) client.ExecuteAsync(req, Sub(res) Console.WriteLine(res.Content) 结束子)
于 2013-10-03T09:14:49.533 回答
0
string email = txtEmailAddress.Text;
string password = txtPassword.Text;
string confirmPassword = txtConfirmPassword.Text;
string dd = txtDD.Text;
string mm = txtMM.Text;
string yyyy = txtYYYY.Text;
UserDetails ud = new UserDetails
{
DateofBirth = DateTime.Now.ToString(),
EmailAddress=email,
Password=password,
ProfileImage="",
UserID="0"
};
WebClient wc = new WebClient();
wc.Headers["Content-Type"] = "application/json";
MemoryStream ms = new MemoryStream();
DataContractJsonSerializer serializertoUpload = new DataContractJsonSerializer(typeof(UserDetails));
serializertoUpload.WriteObject(ms, ud);
string data = Encoding.UTF8.GetString(ms.ToArray(), 0, (int)ms.Length);
wc.UploadStringAsync(new Uri("<wcfserviceurl>"), "POST", data);
这是 UserDetails 类的定义
public class UserDetails
{
public string UserID { get; set; }
public string EmailAddress { get; set; }
public string Password { get; set; }
public string DateofBirth { get; set; }
public string ProfileImage { get; set; }
}
于 2017-04-07T11:56:37.483 回答
-1
public async void makeRequest(String resourceUri)
{
HttpClient httpClient = new HttpClient();
try
{
HttpResponseMessage response = await httpClient.GetAsync(resourceUri);
}
catch (HttpRequestException hre)
{
Console.Write(hre.Message);
}
catch (TaskCanceledException)
{
}
}
记得添加 Nuget 包:httpclient
于 2013-10-09T09:11:30.270 回答