C# Web请求与POST编码问题

发布于 2024-09-04 08:13:09 字数 835 浏览 7 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆蝶 2024-09-11 08:13:09

示例代码具有误导性,因为 ContentType 设置为 application/x-www-form-urlencoded 但实际内容是纯文本。 application/x-www-form-urlencoded 是一个像这样的字符串:

name1=value1&name2=value2

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:

name1=value1&name2=value2

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.

没有你我更好 2024-09-11 08:13:09

正如 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 the name1=value1&name2=value2 string.

This page was helpful: http://geekswithblogs.net/rakker/archive/2006/04/21/76044.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文