Reputation: 3979
Iam trying to encode a url, so that the HttpWebRequest is fine with characters like &.
So google bring me up to this:
url = HttpUtility.UrlEncode(url);
But this makes the whole url unuseable. Iam getting Status-Error: Invalid Operation from Web-Server.
I got this url before iam using encoding:
http://jira-test.myServer.de/rest/api/2/search?jql=labels = "F&E"
After encoding i got this:
http%3a%2f%2fjira-test.brillux.de%2frest%2fapi%2f2%2fsearch%3fjql%3dlabels+%3d+%22F%26E%22
What iam doing wrong? In my opinion it shouldn't replace the // after http and so on... Or is there another way to handle this issue?
Info:
Uri.EscapeDataString();
gives me the same result.
Upvotes: 0
Views: 225
Reputation: 54877
You should only be encoding the values of your query string, not the entire URI:
var uri = "http://jira-test.myServer.de/rest/api/2/search?jql=" +
HttpUtility.UrlEncode("labels = \"F&E\"");
// Result: http://jira-test.myServer.de/rest/api/2/search?jql=labels+%3d+%22F%26E%22
The proper way to construct this:
// Construct query string using HttpValueCollection, which handles escaping:
var queryString = HttpUtility.ParseQueryString(string.Empty);
queryString.Add("jql", "labels = \"F&E\"");
// Combine base URI with query string through UriBuilder:
var uriBuilder = new UriBuilder("http://jira-test.myServer.de/rest/api/2/search");
uriBuilder.Query = queryString.ToString();
// Get string representation:
string uri = uriBuilder.ToString();
// Result: http://jira-test.myserver.de:80/rest/api/2/search?jql=labels+%3d+%22F%26E%22
Upvotes: 2