Windows phone应用程序,Windows. web . http . httpclient post示例
本文关键字:Windows httpclient post 示例 http web phone 应用程序 | 更新日期: 2023-09-27 18:04:35
长时间搜索关于如何使用Windows.Web.Http.HttpClient或httpclient(那不是system.net.httpclient !!)进行post调用参数的示例,有人有吗?
从昨天开始,我已经找到了如何使用Windows.Web.Http.HttpClient:
Windows.Web.Http.HttpClient oHttpClient = new Windows.Web.Http.HttpClient();
Uri uri= ... // some Url
string stringXml= "..."; // some xml string
HttpRequestMessage mSent = new HttpRequestMessage(HttpMethod.Post, uri);
mSent.Content =
new HttpStringContent(String.Format("xml={0}", stringXml),
Windows.Storage.Streams.UnicodeEncoding.Utf8);
HttpResponseMessage mReceived = await oHttpClient.SendRequestAsync(mSent,
HttpCompletionOption.ResponseContentRead);
// to get the xml response:
if (mReceived.IsSuccessStatusCode)
{
string strXmlReturned await mReceived.Content.ReadAsStringAsync();
}
我没有这样做与HtppClient但我做了WebRequest和json参数在请求体:
WebRequest request = WebRequest.CreateHttp(url);
request.Method = "POST";
request.ContentType = "application/json";
using (var stream = await Task.Factory.FromAsync<Stream>(request.BeginGetRequestStream, request.EndGetRequestStream, null))
{
string postData = JsonConvert.SerializeObject(requestData);
byte[] postDataAsBytes = Encoding.UTF8.GetBytes(postData);
await stream.WriteAsync(postDataAsBytes, 0, postDataAsBytes.Length);
}
using (var response = await Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null))
{
return await ProcessResponse(response);
}
顺便说一句,我把这段代码添加到一个便携式库中,现在我可以在Windows Phone 8、8.1和Windows 8.1上使用它了。