简体   繁体   中英

Preserve an escaped Uri with HttpClient

I'm trying to use HttpClient to create a GET request with the following Uri:

http://test.com?action=enterorder&ordersource=acme&resid=urn%3Auuid%3A0c5eea50-9116-414e-8628-14b89849808d As you can see, the resid param is escaped with %3A, ie the ":" character.

When I use this Uri in the HttpClient request, the url becomes:

http://test.com?action=enterorder&ordersource=acme&resid=urn:uuid:0c5eea50-9116-414e-8628-14b89849808d and I receive an error from the server because %3A is expected.

Anyone have any clue on what to do to preserve the escaped Uri when sending the request? It seems HttpClient always unescaped characters on the string before sending it. Here is the code used:

Uri uri = new Uri("http://test.com?action=enterorder&ordersource=acme&resid=urn%3Auuid%3A0c5eea50-9116-414e-8628-14b89849808d");
using (HttpClient client = new HttpClient())
{
   var resp = client.GetAsync(uri);
   if (resp.Result.IsSuccessStatusCode)
      {
        var responseContent = resp.Result.Content;
        string content = responseContent.ReadAsStringAsync().Result;
      }
}

You may want to test in .NET 4.5 as a bunch of improvements were made to Uri parsing for escaped chars.

You can also check out this SO question: GETting a URL with an url-encoded slash which has a hack posted that you can use to force the URI to not get touched.

As a workaround you could try to encode this url part again to circumvent the issue. %3A would become %253A

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM