2016-02-04 115 views
0

我已经从Windows Phone 8 Silverlight迁移到Windows Phone 8.1 WinRT。 HttpWebRquestWebClient用于WP 8 Silverlight。 Wp的8.0 Silverlight中,我已经使用Web客户端..发送请求并获取Windows Phone 8.1的响应WinRT

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方法。我跟着一些例子,得到这个code..which不工作..

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格式。

我不熟悉HTTP内容参数PostAynsc()方法。

通过几个链接。无法获得任何帮助。 如何实现它..

+0

1您的网址无效首先检查您的网址 –

+0

[如何使用Windows.Web.Http.HttpClient(XAML)连接到HTTP服务器) ](https://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn440594.aspx) –

+0

@Umar Ali该链接清楚地说明了获取方法请求..我的要求是仅使用Post方法。 。谢谢 –

回答

0
有帮助

我用这个实用方法POST 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); 
+0

检查我更新的问题。 –

+0

我已更新答案。 –

+0

对于Url参数,我将通过URL.for Json参数..我必须通过.. @ M.Hassan –

0

我用这样的事情与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而不是无效,所以我可以等待结果,后来我用另一种方法反序列化它

相关问题