Search code examples
c#.netweblinkedin-api

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?


Solution

  • 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.