没有编码的HTTP请求

时间:2012-07-27 17:15:19

标签: c#

我需要发送一个没有编码的http请求(我必须使用一个愚蠢的供应商API,如果你对网址进行编码就会中断)

所以目前我有

    string address = "https://www.eco-bb.bt.com/bbxml33/batchstatus.asp?b_customerId=[O/M12346800]&batchid=[" + batchID + "]";
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);

哪个产生

  

POST   https://www.eco-bb.bt.com/bbxml33/Upload.asp?b_customerId=%5BO/M12346800%5D

我希望它能够产生

  

POST   https://www.eco-bb.bt.com/bbxml33/Upload.asp?b_customerId= [O / M12346800]

我有点难过。有关如何进行的任何建议吗?

3 个答案:

答案 0 :(得分:3)

尝试这个,你创建一个Uri对象并保证你已经转义字符串的构造函数(你没有:p)

string address = "https://www.eco-bb.bt.com/bbxml33/batchstatus.asp?b_customerId=[O/M12346800]&batchid=[" + batchID + "]";
Uri uri = new Uri(address, true);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); 

答案 1 :(得分:2)

如果您运行的是.NET 4或更高版本,则可以添加以下config

<configuration>
  <uri>
    <schemeSettings>
      <add name="http" genericUriParserOptions="DontUnescapePathDotsAndSlashes"/>
    </schemeSettings>
  </uri>
</configuration>

答案 2 :(得分:0)

好的,作为一个恐龙,我记得在过去你有时会通过用你自己的特定名称替换特殊字符然后在服务器端解析它们来手动编码你的URL数据。如果这是您的供应商构建他们的网络应用程序的方式,那么他们只需要告诉您他们期望的特殊字符替换例如。

http://www.this.com?variable=[12] 

可能会被视为;

http://www.this.com?variable=lBracket12rBracket

另外,在没有HTTPS的情况下调用他们的服务会产生一些有趣的JavaScript函数!

 <script> 
   function Homepage(){
<!--
// in real bits, urls get returned to our script like this:
// res://shdocvw.dll/http_404.htm#http://www.DocURL.com/bar.htm 

    //For testing use DocURL = "res://shdocvw.dll/http_404.htm#https://www.microsoft.com/bar.htm"
    DocURL=document.URL;

    //this is where the http or https will be, as found by searching for :// but skipping the res://
    protocolIndex=DocURL.indexOf("://",4);

    //this finds the ending slash for the domain server 
    serverIndex=DocURL.indexOf("/",protocolIndex + 3);

    //for the href, we need a valid URL to the domain. We search for the # symbol to find the begining 
    //of the true URL, and add 1 to skip it - this is the BeginURL value. We use serverIndex as the end marker.
    //urlresult=DocURL.substring(protocolIndex - 4,serverIndex);
    BeginURL=DocURL.indexOf("#",1) + 1;
    urlresult=DocURL.substring(BeginURL,serverIndex);

    //for display, we need to skip after http://, and go to the next slash
    displayresult=DocURL.substring(protocolIndex + 3 ,serverIndex);
    InsertElementAnchor(urlresult, displayresult);
}

function HtmlEncode(text)
{
    return text.replace(/&/g, '&amp').replace(/'/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}

function TagAttrib(name, value)
{
    return ' '+name+'="'+HtmlEncode(value)+'"';
}

function PrintTag(tagName, needCloseTag, attrib, inner){
    document.write( '<' + tagName + attrib + '>' + HtmlEncode(inner) );
    if (needCloseTag) document.write( '</' + tagName +'>' );
}

function URI(href)
{
    IEVer = window.navigator.appVersion;
    IEVer = IEVer.substr( IEVer.indexOf('MSIE') + 5, 3 );

    return (IEVer.charAt(1)=='.' && IEVer >= '5.5') ?
        encodeURI(href) :
        escape(href).replace(/%3A/g, ':').replace(/%3B/g, ';');
}

function InsertElementAnchor(href, text)
{
    PrintTag('A', true, TagAttrib('HREF', URI(href)), text);
}

//-->
</script>