HttpWebRequest的网址被特殊字符截断 [英] Url for HttpWebRequest truncated by special characters

查看:239
本文介绍了HttpWebRequest的网址被特殊字符截断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调用的Web服务的URL包含一个包含自由格式文本的参数.我不确定为什么要这样设计,因为它是使用POST发送的,并且包含许多字段作为POST的一部分.但是,这给我带来了麻烦.

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.

对于某些字符,例如井号和< >,当遇到问题字符时,URL将被截断.我正在对该参数的文本进行HTML编码,但问题仍然存在. 我可以看到诸如>之类的特殊字符已被编码为gt;之类的东西.我认为编码字符串中的分号某种程度上是个问题.

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.

我在服务器上放了一个嗅探器,以接收传入的请求,然后看到URL被截断了.

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

代替:

    ...?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())
    {
    ...
    }

推荐答案

哈希(#)符号之后的所有内容都不会发送到服务器.浏览器和页面上的脚本使用它来表示页面上的位置或其他含义.删除哈希符号或对其进行URL编码(%23),以将其发送到服务器.

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.

此行:

strURL = strStuff + "?extraData=" + System.Web.HttpUtility.HtmlEncode(strExtra);

应该是

strURL = strStuff + "?extraData=" + Server.UrlEncode(strExtra);

当使用url中的数据时,

对html进行编码是没有用的.

encoding for html is useless when using the data in a url.

这篇关于HttpWebRequest的网址被特殊字符截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆