长时间使用Windows.Web.Http.HttpClient oHttpClient(不是System.Net.Http.HttpClient !!)搜索有关如何使用参数进行帖子调用的示例,是否有人有? Microsoft样本从不使用参数,我可以看到。
答案 0 :(得分:3)
从昨天开始,我已经找到了如何使用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();
}
答案 1 :(得分:0)
我还没有用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);
}
BTW,我将此代码添加到便携式库中,现在我可以在Windows Phone 8,8.1和Windows 8.1上使用它。