HttpWebRequest 的 URL 被特殊字符截断
我需要调用的 Web 服务的 URL 包含一个包含自由格式文本的参数。我不知道为什么它是这样设计的,因为它是使用 POST 发送的,并且包含许多字段作为 POST 的一部分。但是,这给我带来了一个问题。
对于某些字符,如井号和 < >,当 URL 遇到问题字符时,URL 将被截断。我正在对参数的文本进行 HTML 编码,但问题仍然存在。 我可以看到特殊字符,例如 >被编码为类似 gt; 的内容。我认为编码字符串中的分号在某种程度上是一个问题。
我在服务器上放置了一个嗅探器来接收传入请求,我发现 URL 已被截断。
在服务器上我看到类似:
...?extraData=kjfkfjslkj
而不是:
...?extraData=kjfkfjslkj#kfjkdlsfj
代码是这样的:
using System.Web;
....
String strExtra="kjfkfjslkj#kfjkdlsfj";
strURL = strStuff + "?extraData=" + System.Web.HttpUtility.HtmlEncode(strExtra);
HttpWebRequest oRequest = (HttpWebRequest)WebRequest.Create(new Uri(strURL));
oRequest.Method = httpMethod;
oRequest.ContentType = "application/atom+xml";
...
using (WebResponse oResponse = oRequest.GetResponse())
{
...
}
The URL of a Web Service that I need to call includes a parameter that includes free form text. I'm not sure why it was designed that way since it is sent using POST and includes many fields as part of the POST. But, it is causing me a problem.
For some characters like the pound sign and < >, the URL is truncated when it hits the problem character. I'm HTML encoding the text for the parameter, but the problem remains.
I can see special characters like > are being encoded to something like gt;. I'm thinking that the semicolon in the encoded string is somehow a problem.
I put a sniffer receiving the incoming request at the server and I see there that the URL has been truncated.
At the server I see something like:
...?extraData=kjfkfjslkj
instead of:
...?extraData=kjfkfjslkj#kfjkdlsfj
The code is something like this:
using System.Web;
....
String strExtra="kjfkfjslkj#kfjkdlsfj";
strURL = strStuff + "?extraData=" + System.Web.HttpUtility.HtmlEncode(strExtra);
HttpWebRequest oRequest = (HttpWebRequest)WebRequest.Create(new Uri(strURL));
oRequest.Method = httpMethod;
oRequest.ContentType = "application/atom+xml";
...
using (WebResponse oResponse = oRequest.GetResponse())
{
...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哈希 (
#
) 符号之后的所有内容都不会发送到服务器。浏览器和页面上的脚本使用它来表示页面上的位置或其他含义。删除哈希符号或对其进行 url 编码 (%23
) 以将其发送到服务器。这行:
should be encoding for html 是没有用的。
当使用 url 中的数据时,
Everything after the hash (
#
) sign is not sent to the server. It's used by the browser and scripts on the page to denote a location on a page, or some other significance. Remove the hash sign or url encode it (%23
) to send it to the server.This line:
should be
encoding for html is useless when using the data in a url.