[英]HttpUtility.UrlEncode unexpected output
Iam trying to encode a url, so that the HttpWebRequest is fine with characters like &. 我试图对网址进行编码,以便HttpWebRequest可以使用&之类的字符。
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: 我在使用编码的iam之前获得了这个网址:
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? 在我看来,它不应该在http之后替换//等,还是有其他方法可以解决此问题?
Info: 信息:
Uri.EscapeDataString();
gives me the same result. 给我相同的结果。
You should only be encoding the values of your query string, not the entire URI: 您应该只编码查询字符串的值,而不是整个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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.