[英]Send request & Get Response in Windows Phone 8.1 WinRT
我已经从Windows Phone 8 Silverlight迁移到Windows Phone 8.1 WinRT。 WP 8 Silverlight中使用的HttpWebRquest
和WebClient
。 WP 8.0 silverlight,我用过webclient。
webClntRech.DownloadStringAsync(new Uri("http://wallet.net.co.in/services/bi/rechargedownload/01/0001/" + DateTime.Now.ToString("ddMMyyhhmmssms") + "/504434"));
webClntRech.DownloadStringCompleted += webClntRech_DownloadStringCompleted;
我想要WInrt 8.1中的类似功能
我在WinRT中找不到WebClient
。 我遇到了HttpClient
。
根据需求,我们必须对所有请求实施POST方法。 我遵循了一些示例,并获得了此代码。
HttpClient client = new HttpClient();
string ResponceResult = await client.PostAsync("http://wallet.net.co.in/services/bi/rechargedownload/01/0001/" + DateTime.Now.ToString("ddMMyyhhmmssms") + "/504434",);
MessageDialog m = new MessageDialog(ResponceResult);
await m.ShowAsync();
响应将采用Json格式。
我对PostAynsc()
方法的HTTP Content参数不熟悉。
通过几个链接。 无法获得任何帮助。 如何实施
首先,您需要更改您的网址,即: http : //services.groupkt.com/country/get/all
然后试试这个
发送Http请求以在Windows Phone 8.1上休息api
我希望这对您有帮助
我使用此实用程序方法发布JSON
async void PostJson(string URL, string json)
{
HttpClient httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromMinutes(5);
httpClient.MaxResponseContentBufferSize = 25600000;
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/json");
HttpResponseMessage response = await httpClient.PostAsync(new Uri(URL), new StringContent(json, Encoding.UTF8, "application/json"));
response.EnsureSuccessStatusCode();
string responseAsText = await response.Content.ReadAsStringAsync();
Dictionary<string, string> responseJson = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseAsText);
}
更新资料
为了仅从URL获取JSON,可以使用以下代码:
var httpClient = new HttpClient();
var response = await httpClient.GetStringAsync(url);
// Deserialize JSON
MyObject myObject = JsonConvert.DeserializeObject<MyObject>(response);
我在HttpClient
使用了类似的方法
public async Task<string> httpClient(object param, Uri targetUri, string key)
{
using(HttpClient client = new HttpClient())
{
string jsonData = JsonConvert.SerializeObject(param);
FormUrlEncodedContent content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>(key, jsonData)
});
HttpResponseMessage response = await client.PostAsync(targetUri, content);
string result = await response.Content.ReadAsStringAsync();
return result;
}
}
您可以根据需要进行任何修改
我使用Task
而不是void来等待结果,然后我用另一种方法将其反序列化
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.