1

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

所以目前我有

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

哪个生产

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

我希望它产生

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

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

4

3 回答 3

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); 
于 2012-07-27T17:28:44.830 回答
2

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

<configuration>
  <uri>
    <schemeSettings>
      <add name="http" genericUriParserOptions="DontUnescapePathDotsAndSlashes"/>
    </schemeSettings>
  </uri>
</configuration>
于 2012-07-27T18:03:12.180 回答
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>
于 2012-07-27T17:56:07.580 回答