none
.NET 4.5 new Uri unescaping double dot when there's inverted question mark

    Question

  • I have the next problem. I'm trying to post the an status to twitter that contains an inverted question mark and a link. For instance, "¿http.www.google.com". 

    I'm using twitterizer for posting to twitter but when I try to post it, twitter response is "Could not authenticate you". Debugging twitterizer I have discovered that the problem is that when the Uri for making the request to Twitter is being built with the next string:

        string correctUri = "http://api.twitter.com/1.1/statuses/update.json?status=%C2%BFhttp%3A%2F%2Fwww.google.com";

    That's correct, every special character is escaped and everything is fine. But when new Uri is done with that string, then it comes the problem:

        Uri uri =new Uri(correctUri);

    Because the AbsoluteUri value is http://api.twitter.com/1.1/statuses/update.json?status=%C2%BFhttp:%2F%2Fwww.google.es . As you can see, it shows "http:" and that's exactly the problem.

    I have tried the same exact code under .NET 4.5 and .NET 4 and it's only a problem under .NET 4.5. I also tried to post the same text under the two versions of .NET and it only works under .NET 4 and the only difference at the http request made to twitter (captured with fiddler) is the double dot. 

    I have discovered also that the problem only happens when you put an inverted question mark (%C2%BF in UTF-8) at the status. But when you don't put it, there's no problem at all.

    Is there any solution to this problem? I have tried so many things, but nothing works.
    Friday, September 13, 2013 7:01 AM

All replies

  • Hi aamarber,

    Welcome to MSDN forums.

    Thanks for the second reply. And Sorry for the delay reply. A few days ago I went to the hospital.

    I would suggest you have a look at the reference below.

    http://msdn.microsoft.com/en-us/library/system.uri.originalstring(v=vs.110).aspx

    Now you can try the workaround below. You can use the Uri.OriginalString Property instead of ToString and AbsoluteUri property.

    The value returned by this property differs from ToString and AbsoluteUri. ToString returns the canonically unescaped form of the URI. AbsoluteUri returns the canonically escaped form of the URI.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace URLTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                string url = @"http://api.twitter.com/1.1/statuses/update.json?status=%C2%BFhttp%3A%2F%2Fwww.google.com";
                Console.WriteLine(url);
                Uri uri = new Uri(url);
                Console.WriteLine(uri.OriginalString);
            }
        }
    }
    

    the output :

    If the issue persists, just feel free to let me know.

    Regards,

    Damon


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Sunday, September 15, 2013 3:25 AM
    Moderator
  • Hi,

    Thanks a lot Damon for answering, but none of your links is my problem. My specific problem is that, I don't know why, when I make new Uri of a string that contains a %C2%BF (unicode character for ¿) and %3A the constructor of uri unescapes the %3A.

    This is the example:

    - Before new Uri (Correct): 
    http://api.twitter.com/1.1/statuses/update.json?status=%C2%BFhttp%3A%2F%2Fwww.google.com

    - After new Uri (Wrong):
    http://api.twitter.com/1.1/statuses/update.json?status=%C2%BFhttp:%2F%2Fwww.google.es

    The http%3A has been unescaped to http: That is not happening under .NET 4.0, there's any workaround? 

    Wednesday, September 18, 2013 1:01 PM
  • Hi aamarber,

    I have updated  my first reply, please check if it works or not.

    Please let me know the result. Thanks.

    I  will temporarily mark my first response as an answer. You can unmark it if they provide no help.

    Regards,

    Damon


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.



    Friday, September 20, 2013 9:40 AM
    Moderator
  • Hi again Damon,

    Yes, debugging I realized of that fact, that OriginalString stores the correct string without any transformation. But the next problem is that I need to make an HttpWebRequest and  In fact, I tried to make a workaround similar to the one that you are saying because I have the next code:

    request = (HttpWebRequest)WebRequest.Create(correctString);

    But even that I'm using the correctString at the Create method it just make the wrong http request. I have debugged and I have seen that inside the variable request there is an incorrect uri, because of the new Uri constructor.

    I don't know if I'm explaining myself correctly, but anyway, thanks a lot for taking interest in my issue!

    Monday, September 23, 2013 9:29 AM