10

I am working on an asp.net mvc web application. now i have a value for operating system which equal to :-

enter image description here

and i am using the following code to build a url containing the above value as follow:-

var query = HttpUtility.ParseQueryString(string.Empty);
query["osName"] = OperatingSystem;


var url = new UriBuilder(apiurl);
url.Query = query.ToString();
string xml = client.DownloadString(url.ToString());

but the generated url will contain the following value for the operating system :-

osName=Microsoft%u00ae+Windows+Server%u00ae+2008+Standard

so the UriBuilder will encode the registered sign as %u00ae+ which is wrong since when i try to decode the %u00ae+ using online site such as http://meyerweb.com/eric/tools/dencoder/ it will not decode %u00ae+ as register sign ??? so can anyone adivce on this please, how i can send the registered sign inside my url ? is this a problem within UrilBuilder ?

Thanks

EDIT This will clearly state my problem.. now i pass the asset name valueas £ ££££££ now the query["assetName"] will get the correct value.. but inside the query the value will get encoded wrongly (will not be encoded using UTF8 )!!!

enter image description here

John John
  • 1
  • 72
  • 238
  • 501

2 Answers2

5

Try this:

url.Query = Uri.EscapeUriString(HttpUtility.UrlDecode(query.ToString()));

As mentioned here HttpUtility.ParseQueryString() always encodes special characters to unicode

Community
  • 1
  • 1
Sergey Lobanov
  • 365
  • 3
  • 14
  • but can you advice what is the problem in my case ? – John John Aug 08 '16 at 17:11
  • 2
    To make a correct URL (RFC3986 standard) you should use [Uri.EscapeDataString](https://msdn.microsoft.com/en-us/library/system.uri.escapedatastring.aspx), otherwise special unicode characters will be encoded by default like in your question. – Sergey Lobanov Aug 09 '16 at 15:53
  • But if i replace "url.Query = query.ToString();" with "Uri.EscapeUriString(HttpUtility.UrlDecode(query.ToString()));" will i face any problems??... now seems using "Uri.EscapeUriString(HttpUtility.UrlDecode(query.ToString()));" will fix my issue for the "registered sign" but will it cause other characters to fail ... – John John Aug 10 '16 at 15:10
  • now using Uri.EscapeUriString(HttpUtility.UrlDecode(query.ToString())); i will be facing is issue .. now let say i want to pass value such as "&+&+" now the generated url will look as follow &assetName=&+&+ .. so it will assume that the assetName parameters is empty !! – John John Aug 10 '16 at 16:53
  • `Uri.EscapeDataString();` will do the job. For example, `Uri.EscapeDataString("&+&+Test©");` will result in `"%26%2B%26%2BTest%C2%A9"` . Everything is very tricky around UrlEncoding in .NET. You may find this question helpful: http://stackoverflow.com/questions/575440/url-encoding-using-c-sharp P.S. you may not use `HttpUtility.UrlDecode` at all, just use `Uri.EscapeDataString` – Sergey Lobanov Aug 11 '16 at 13:27
  • so my code should look as follow :- "url.Query = Uri.EscapeDataString(query.ToString());"??? – John John Aug 12 '16 at 12:43
  • i think your are just providing replies without even trying it.. now in my case the value inside the Uri.EscapeDataString() will never be "&+&+Test©" .. because i am building the query string using "query["osName"] = OperatingSystem;" so the value ""&+&+Test©"" will alreay be encoded when it reaches Uri.EscapeDataString()!!! – John John Aug 12 '16 at 13:07
  • well, i tested my replies. This will work fine: `Uri.EscapeDataString(HttpUtility.UrlDecode("Microsoft%u00ae+Windows+Server%u00ae+2008+Standard", Encoding.UTF8))` and result will be `Microsoft%C2%AE%20Windows%20Server%C2%AE%202008%20Standard`, also the `&` sumbol will convert into `%26` perfectly with this code. – Sergey Lobanov Aug 16 '16 at 07:57
0

Use Uri.EscapeDataString() to escape data (containing any special characters) before adding it in the Query String

Pradeep
  • 1,108
  • 1
  • 15
  • 31
  • ,,, So can you advice how i can use this method inside my code ??? you mean to replace Uri.EscapeUriString(HttpUtility.UrlDecode(query.ToString())); with Uri.EscapeDataString(HttpUtility.UrlDecode(query.ToString())); – John John Aug 12 '16 at 12:39
  • query["osName"] = OperatingSystem; var url = new UriBuilder(apiurl); url.Query = Uri.EscapeDataString(query.ToString()); – Pradeep Aug 12 '16 at 12:47
  • i am not sure if you tried your appraoch.. now using your appraoch the url inside the downloadstring will look as follow "http://********:8080/AssetServlet?username%3DadministratorassetName%3Dtest%2B%25u00a3%2Btest123%26" so the server will reply back that username can not be empty !!! – John John Aug 12 '16 at 13:05
  • Could you provide me the data that you want to put in the query string. And I have tried my approach with your given data in the question above and it works fine. It would be very helpful if you update your question with what exactly you want – Pradeep Aug 12 '16 at 13:36
  • now my question is clear ... here is my code "query["osName"] = OperatingSystem; var url = new UriBuilder(apiurl);url.Query = query.ToString();" now any non-ASCIII characters will be mapped wrongly... i think you are missing that query["osName"] = OperatingSystem; will encode any non-ASCII characters using ISO-... instead of UTF8 ... so if i use Uri.EscapeDataString() will not be able to solve the problem because it will contain wrongly encoded data ,, in other words it will contain any non-ASCII characters encoded using ISO instead of UTF8 .. – John John Aug 12 '16 at 13:56
  • can you check my edit to the original question... i provide more info about my problem – John John Aug 12 '16 at 14:13