![](/img/trans.png)
[英]Where is the “WebClient.UploadString” Method in Windows Phone 7?
[英]Replace WebClient.UploadString() to HttpClient.PostAsync
我想在我的代码中将WebClient替换为HttpClient。 我必须在HttpClient中使用什么HttpContent来替换WebClient.UploadString? 我的WebClient代码:
string data = string.Format("name={0}&warehouse={1}&address={2}", name, shop.Warehouse.Id, shop.Address);
using (var wc = new WebClient()) {
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
wc.Encoding = Encoding.UTF8;
string fullUrl = BaseUrl + url;
string response = wc.UploadString(fullUrl, data);
// ...
}
您可以构造postdata并在FormUrlEncodedContent的实例中使用它,如下所示:
// This is the postdata
var data = new List<KeyValuePair<string, string>>();
data.Add(new KeyValuePair<string, string>("Name", "test"));
HttpContent content = new FormUrlEncodedContent(data);
此页面上指定了解决方案:
如何使用System.Net.HttpClient发布复杂类型?
您可以决定异步或同步发布它,例如:
HttpResponseMessage x = await httpClient.PostAsync(fullUrl, content);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.