使用 URI 类时保持 url 编码 [英] Keep url encoded while using URI class

查看:37
本文介绍了使用 URI 类时保持 url 编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 LinkedIn 获取公开个人资料信息.为了实现这一点,我必须提供
http://api.linkedin.com/v1/people/url=public-profile-url,其中 public-profile-url 必须是 URL 编码的.

问题是 .NET 类(例如 HttpClient、WebRequest 等)使用 Uri 类,该类似乎规范化"了所提供的 URL,因此我无法发送正确格式的请求.

URI 必须是:

http:///api.linkedin.com/v1/people/url=http%3a%2f%2fwww.linkedin.com%2fin%2fiftachragoler

但是是:

http://api.linkedin.com/v1/people/url=http://www.linkedin.com/in/iftachragoler

通过这种方式,我收到来自 LinkedIn 的错误请求".

有什么办法可以让 Uri/UriBuilder 不解码那个 URL?

解决方案

Microsoft connect 上有相关报道.由于安全原因,默认情况下不允许使用转义斜杠.

http://connect.microsoft.com/VisualStudio/feedback/details/94109/

从那里引用:

<块引用>

我尝试使用 LinkedIn api,为此我需要以下链接:http://api.linkedin.com/v1/people/url=http%3A%2F%2Fwww.linkedin.com%2Fin%2Fyourlinkedinname:public

如您所见,需要对 url 字段进行转义.如何解决这个问题?

答案:

<块引用>

我们目前不允许转义斜线和点出现在路径,因为这是在 URI 时攻击服务器的常用方法方案支持路径压缩.

但是有带有变通方法的选项卡..NET 4 的其中之一是添加 app.config:

对于.NET 4.0,你可以通过配置文件来控制:

http://msdn.microsoft.com/en-us/library/bb882619.aspx

http://msdn.microsoft.com/en-us/library/ee656539.aspx

<预><代码><配置><uri><方案设置><清除/><add name="http" genericUriParserOptions="DontUnescapePathDotsAndSlashes"/></schemeSettings></uri></配置>

对于 .NET,在 .NET 是带有参数dontEscape"的 Uri 类的构造函数之前.对于 .NET 4,它已过时.

I'm trying to get public profile information from LinkedIn. To achieve this I have to provide
http://api.linkedin.com/v1/people/url=public-profile-url, where public-profile-url MUST be URL encoded.

The issue is that .NET classes such as HttpClient, WebRequest etc use Uri class which seems to "canonize" the provided URL, so that I can't get the proper formatted request sent.

The URI must be:

http://api.linkedin.com/v1/people/url=http%3a%2f%2fwww.linkedin.com%2fin%2fiftachragoler

but is:

http://api.linkedin.com/v1/people/url=http://www.linkedin.com/in/iftachragoler

In this way, I get 'Bad Request' from LinkedIn.

Is there any way I can have Uri/UriBuilder not to decode that URL?

解决方案

There is report about that on Microsoft connect. By default escaped slashes not allowed due to security reasons.

http://connect.microsoft.com/VisualStudio/feedback/details/94109/

Cites from there:

I try to use the LinkedIn api, for which I need the following link: http://api.linkedin.com/v1/people/url=http%3A%2F%2Fwww.linkedin.com%2Fin%2Fyourlinkedinname:public

As you can see the url field needs to be escaped. How to solve this?

Answer:

We currently don't allow escaped slashes and dots to appear in the path because this is a common way to attacker a server when the URI scheme supports path compression.

But there is tab with workarounds. One of them for .NET 4 is to add app.config:

For .NET 4.0, you can control this through the config file:

http://msdn.microsoft.com/en-us/library/bb882619.aspx

http://msdn.microsoft.com/en-us/library/ee656539.aspx

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

For .NETs before .NET was constructor for Uri class with parameter "dontEscape". For .NET 4 it's obsolete.

这篇关于使用 URI 类时保持 url 编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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