0

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

bagofmilk
  • 1,492
  • 8
  • 34
  • 69
  • 1
    `;` 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 06 '18 at 16:12
  • 1
    Are you sure it fails with encoded semicolons? – Hans Kesting Aug 06 '18 at 16:24
  • 1
    @HansKesting yes. The system is old - the developers are just now looking to convert this POS Silverlight App into HTML5 standards – bagofmilk Aug 06 '18 at 16:26

1 Answers1

3

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?

Adam Schiavone
  • 2,412
  • 3
  • 32
  • 65
  • [`HttpUtillity.UrlEncode`](https://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode(v=vs.110).aspx) would be a better option than string replace – Liam Aug 06 '18 at 16:18
  • 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 06 '18 at 16:20
  • 3
    @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? –  Aug 06 '18 at 16:25