Literal ampersands in the System.Uri query string

I am working on a client application that uses the recovery service to search for companies by name. It is very important that ampersand literals are included in my queries, as this character is quite common in company names.

However, whenever I pass% 26 (URI with an ampersand character) to System.Uri , it will convert it back to a regular ampersand character! On closer inspection, only two characters that were not returned are the hash (% 23) and the percentage (% 25).

Suppose I want to find a company called Pier and Pier:

 var endPoint = "http://localhost/companies?where=Name eq '{0}'"; var name = "Pierce & Pierce"; Console.WriteLine(new Uri(string.Format(endPoint, name))); Console.WriteLine(new Uri(string.Format(endPoint, name.Replace("&", "%26")))); Console.WriteLine(new Uri(string.Format(endPoint, Uri.EscapeUriString(name)))); Console.WriteLine(new Uri(string.Format(endPoint, Uri.EscapeDataString(name)))); 

All four of the above combinations return:

 http://localhost/companies?where=Name eq 'Pierce & Pierce' 

This causes server-side errors because the ampersand is (correctly) interpreted as a delimiter of the request arguments. I really need to return the original string:

 http://localhost/companies?where=Name eq 'Pierce %26 Pierce' 

How can I get around this behavior without completely dropping System.Uri ? I cannot replace all ampersands with% 26 at the last moment, because usually several query arguments will be involved, and I do not want to destroy their delimiters.

Note. . A similar problem was discussed in this question , but I specifically refer to System.Uri .

+4
source share
3 answers

Not only ampersand is incorrect in the URL. A valid URL cannot contain spaces.

The EscapeDataString method EscapeDataString great to correctly encode a string, and you must encode the entire value, not just the name:

 Uri.EscapeDataString("Name eq 'Pierce & Pierce'") 

Result:

 Name%20eq%20'Pierce%20%26%20Pierce' 

When creating a Uri using this line, this will be correct. To see the URL, you can use the AbsoluteUri property. If you simply convert the Uri to a string (which calls the ToString method), the URL will be uninsulated and therefore will not display correctly.

+10
source

I am using the same problem. Although the query string is escaped using Uri.EscapeDataString, and the AbsoluteUri property does it correctly, WebBrowser sends Uri in unlimited format.

  currentUri = new System.Uri(ServerAgent.urlBase + "/MailRender?uid=" + Uri.EscapeDataString(uid); 

webBrowser.Navigate (currentUri);

The plus sign ('+') is converted to% 2B, but the server still gets + at the URL, which is then converted to space ('') via a call to HttpServeletRequest.getParameter ()

0
source

I solved this problem by creating a Uri derived class

  class Uri2 : System.Uri { public Uri2(string url) : base(url) { } public override string ToString() { return AbsoluteUri; } } 

Anywhere System.Uri uses the replacement Uri2. I do not know if this is a .NetCF error, WebBrowser should send the URL in encoded format, that is, the value of AbsoluteUri, but not the value from ToString ()

0
source

Source: https://habr.com/ru/post/1310347/


All Articles