C# Keep Semicolon in URI

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



C# Keep Semicolon in URI



I need to keep the semicolon in the URI (DO not escape it). The final url will look something like this:



http://dontclick.com?Product=CON&Properties=color;price;location



I've been messing around with UriBuilder but cannot seem to keep the semicolon.



It keeps escaping ";" as "%3d"


string url = "http://dontclick.com";
string product = "CON";
List<string> props = new List<string>"color", "price", "location";

var uriBuilder = new UriBuilder(url);
var paramValues = HttpUtility.ParseQueryString(uriBuilder.Query);
paramValues.Add("Product", product);
paramValues.Add("PropertyNames", string.Join(";", props));
uriBuilder.Query = paramValues.ToString();

string finalURL = uriBuilder.Query.ToString();

Console.WriteLine(finalURL);

//OUTPUT:
//http://dontclick.com?Product=CON&PropertyNames=color%3dprice&3dlocation



Can anyone help me keep the semicolons?



EDIT
I have a few comments on why I need semicolons as they are illegal characters.



Answer: It is what the URL requires - I cannot control it. I'm querying an external site (I have my own encrypted key for access) that will return an XML file based on the parameters. In the example they explicitly tell us to separate the parameters using semicolons. When I use semicolons - it works, if I let the URL encode the semicolons as %3d - it does not work.



Unfortunately I cannot control this





; is a reserved character in a Url, so it's being encoded. If you control dontclick.com then have your application decode the value.
– TZHX
Aug 6 at 16:12



;


dontclick.com





Are you sure it fails with encoded semicolons?
– Hans Kesting
Aug 6 at 16:24





@HansKesting yes. The system is old - the developers are just now looking to convert this POS Silverlight App into HTML5 standards
– bagofmilk
Aug 6 at 16:26





1 Answer
1



According to the URL specification (The CFG is on page 15/16), a semicolon is an illegal character for a URL, so its being encoded out.



Either the service you're trying to use was designed poorly, or you're trying to do something you really shouldn't.



However, to answer your question:



You can string replace the finalURL:


finalUrl = finalUrl.Replace("%d3", ";");



But again, something smells funky here. Can you tell us why you need this semicolon?





HttpUtillity.UrlEncode would be a better option than string replace
– Liam
Aug 6 at 16:18


HttpUtillity.UrlEncode





I have to query an external system that will produce an XML file based on the query. In the examples that it gives us - it requires a semicolon to represent separate Property Names. Whether or not this is illegal, this is what I have to deal with and I cannot control it.
– bagofmilk
Aug 6 at 16:20





@Liam Isn't the point of that code to change %d3 to ; after it's been encoded? That' make HttpUtillity.UrlEncode a very poor option?
– AndyJ
Aug 6 at 16:25



%d3


;


HttpUtillity.UrlEncode





@Adam - Thank you BTW. Simple solution and works.
– bagofmilk
Aug 6 at 16:28






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard