C# Windows phone应用程序,Windows.Web.Http.HttpClient发布示例
长时间搜索有关如何使用Windows.Web.Http.HttpClient oHttpClient(这不是System.Net.Http.HttpClient!!)使用参数进行post调用的示例,有人有吗?C# Windows phone应用程序,Windows.Web.Http.HttpClient发布示例,c#,dotnet-httpclient,windows-phone-8.1,C#,Dotnet Httpclient,Windows Phone 8.1,长时间搜索有关如何使用Windows.Web.Http.HttpClient oHttpClient(这不是System.Net.Http.HttpClient!!)使用参数进行post调用的示例,有人有吗? 我可以看到,Microsoft示例从不使用参数。我没有使用HtppClient,但我使用了请求正文中的WebRequest和json参数: WebRequest request = WebRequest.CreateHttp(url); request.Method = "POST"; r
我可以看到,Microsoft示例从不使用参数。我没有使用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);
}
WebRequest-request=WebRequest.CreateHttp(url);
request.Method=“POST”;
request.ContentType=“application/json”;
使用(var stream=await Task.Factory.fromsync(request.BeginGetRequestStream,request.EndGetRequestStream,null))
{
string postData=JsonConvert.SerializeObject(requestData);
byte[]postDataAsBytes=Encoding.UTF8.GetBytes(postData);
wait stream.WriteAsync(postDataAsBytes,0,postDataAsBytes.Length);
}
使用(var response=wait Task.Factory.fromsync(request.BeginGetResponse,request.EndGetResponse,null))
{
返回等待处理响应(response);
}
顺便说一句,我将此代码添加到一个便携库中,现在我可以在Windows Phone 8、8.1和Windows 8.1上使用它。从昨天开始,我发现了如何使用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();
}