【发布时间】:2011-02-27 01:49:58
【问题描述】:
在 MSDN 站点上有一个 example of some C# code,它显示了如何使用 POST 数据发出 Web 请求。以下是该代码的摘录:
WebRequest request = WebRequest.Create ("http://www.contoso.com/PostAccepter.aspx ");
request.Method = "POST";
string postData = "This is a test that posts this string to a Web server.";
byte[] byteArray = Encoding.UTF8.GetBytes (postData); // (*)
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream ();
dataStream.Write (byteArray, 0, byteArray.Length);
dataStream.Close ();
WebResponse response = request.GetResponse ();
...more...
标有(*) 的行是令我困惑的行。不应该使用 UrlEncode 方法而不是 UTF8 对数据进行编码吗? application/x-www-form-urlencoded不就是这个意思吗?
【问题讨论】: