【发布时间】:2015-01-21 19:13:56
【问题描述】:
我正在尝试发布到一个网站,其中唯一需要的参数是“描述”。我相信这与我编码数据的方式有关。我错过了什么/以错误的方式解决这个问题吗?
string postData = String.Format("description=" + description + "&");
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
wr.Method = WebRequestMethods.Http.Post;
wr.ContentLength = byteArray.Length;
wr.ContentType = "application/x-www-form-urlencoded";
Stream postStream = wr.GetRequestStream();
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
WebResponse response = await wr.GetResponseAsync();
HttpWebResponse webResponse = (HttpWebResponse)response;
using (var stm = response.GetResponseStream())
{
using (var reader = new StreamReader(stm))
{
var content = await reader.ReadToEndAsync();
reader.Close();
stm.Close();
return content;
}
}
【问题讨论】:
-
为什么不改用HttpClient呢?
-
附带说明一下,您像这样使用
String.Format似乎毫无意义......
标签: c# httpwebrequest http-post