Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
353 views
in Technique[技术] by (71.8m points)

c# - Keep url encoded while using URI class

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...