Your problem is that &
carries special meaning in a query string, namely to separate arguments. The &
in the middle of your perceived artist
parameter is actually being parsed as an argument separator. You need to URL encode the &
when constructing the URL.
For example, one correct query string would be:
?artist=singer1+%26+singer2&song=name+song
(Spaces can usually be encoded either as +
or %20
, but that's a topic for another discussion.)
You can use HttpUtility.UrlEncode()
when building the query string components to ensure that the result is correctly escaped. For example, you could build the query string like this:
static void Main()
{
var values = new NameValueCollection();
values["artist"] = "singer1 & singer2";
values["song"] = "name song";
Console.WriteLine(CreateQueryString(values));
}
static string CreateQueryString(NameValueCollection values)
{
if (values == null) { throw new ArgumentNullException("values"); }
return string.Join(
"&",
values.AllKeys.Select(key => HttpUtility.UrlEncode(key) + "=" +
HttpUtility.UrlEncode(values[key]))
.ToArray());
}
In other words, the issue is occuring at the time you build the query string, not when you are trying to parse it. When parsing it you already have a semantically incorrect query string; there's nothing you can do at this point.