我可以阻止Uri在WebRequest.Create中取消对URL的编码吗? [英] Can I stop Uri from unencoding a url in WebRequest.Create?

查看:260
本文介绍了我可以阻止Uri在WebRequest.Create中取消对URL的编码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码本身并不复杂,只是无法正常工作:

The code itself isn't complex, it's just not working properly:

Uri uri = new Uri("https://www.google.com/webmasters/tools/feeds/sites/http%3A%2F%2Fwww.mydomain.co.uk%2F");
WebRequest.Create(uri);

我从服务器收到一个错误请求",经过大量的挖掘,发现Uri正在变成

I get a "Bad Request" back from the server, and after much MUCH digging, discovered that the Uri is being turned into

https://www.google.com/webmasters/tools/feeds/sites/http%3A//www.mydomain.co.uk/

这不是我要的,所以有点儿发怒

which is not what I asked for, and so it's having a whinge

有办法阻止这种情况吗?

Is there a way to stop this?

推荐答案

在此处找到答案: https://stackoverflow.com/a /10415482/159341

根据在Microsoft Connect上针对此问题的错误报告,此行为是设计使然,但是您可以通过将以下内容添加到app.config或web.config文件中来解决此问题:

According to the bug report for this issue on Microsoft Connect, this behaviour is by design, but you can work around it by adding the following to your app.config or web.config file:

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

(由于WebRequest.Create(string)仅委托给WebRequest.Create(Uri),因此无论调用哪种方法,都需要使用此替代方法.)

(Since WebRequest.Create(string) just delegates to WebRequest.Create(Uri), you would need to use this workaround no matter which method you call.)

这篇关于我可以阻止Uri在WebRequest.Create中取消对URL的编码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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