C# Web请求与POST编码问题
MSDN 网站上有一个一些 C# 代码示例,展示了如何使用 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
所暗示的吗?
On the MSDN site there is an example of some C# code that shows how to make a web request with POST'ed data. Here is an excerpt of that code:
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...
The line marked (*)
is the line that puzzles me. Shouldn't the data be encoded using the UrlEncode method rather than UTF8? Isn't that what application/x-www-form-urlencoded
implies?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
示例代码具有误导性,因为 ContentType 设置为 application/x-www-form-urlencoded 但实际内容是纯文本。 application/x-www-form-urlencoded 是一个像这样的字符串:
UrlEncode 函数用于转义特殊字符,如 '&'和 '=' 因此解析器不会将它们视为语法。它接受一个字符串(媒体类型 text/plain)并返回一个字符串(媒体类型 application/x-www-form-urlencoded)。
Encoding.UTF8.GetBytes 用于将字符串(在本例中为媒体类型 application/x-www-form-urlencoded)转换为字节数组,这正是 WebRequest API 所期望的。
The sample code is misleading, because ContentType is set to application/x-www-form-urlencoded but the actual content is plain text. application/x-www-form-urlencoded is a string like this:
The UrlEncode function is used to escape especial characters like '&' and '=' so a parser doesn't consider them as syntax. It takes a string (media type text/plain) and returns a string (media type application/x-www-form-urlencoded).
Encoding.UTF8.GetBytes is used to convert the string (media type application/x-www-form-urlencoded in our case) into an array of bytes, which is what the WebRequest API expects.
正如 Max Toro 指出的,MSDN 站点上的示例是不正确的:正确的 POST 形式需要对数据进行 URL 编码;由于 MSDN 示例中的数据不包含任何会因编码而更改的字符,因此从某种意义上来说,它们已经被编码了。
正确的代码将对每个名称/值对的名称和值进行
System.Web.HttpUtility.UrlEncode
调用,然后将它们组合到name1=value1&name2=value2
字符串。此页面很有帮助: http://geekswithblogs.net/rakker/archive/2006/04/21/76044.aspx
As Max Toro indicated, the examples on the MSDN site are incorrect: a correct form POST requires the data to be URL encoded; since the data in the MSDN example does not contain any characters that would be changed by encoding, they are, in a sense, already encoded.
The correct code would have a
System.Web.HttpUtility.UrlEncode
call on the names and values of each name/value pair before combining them into thename1=value1&name2=value2
string.This page was helpful: http://geekswithblogs.net/rakker/archive/2006/04/21/76044.aspx