java 在 URL 中使用带有非法字符的 HttpGet
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2923467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use HttpGet with illegal characters in the URL
提问by Catalin Morosan
I am trying to use DefaultHttpClientand HttpGetto make a request to a web service. Unfortunately the web service URL contains illegal characters such as { (ex: domain.com/service/{username}). It's obvious that the web service naming isn't well written but I can't change it.
我正在尝试使用DefaultHttpClient并向HttpGetWeb 服务发出请求。不幸的是,Web 服务 URL 包含非法字符,例如 {(例如:domain.com/service/{username})。很明显,Web 服务命名写得不好,但我无法更改它。
When I do HttpGet(url), I get that I have an illegal character in the url (that is { and }). If I encode the URL before that, there is no error but the request goes to a different URL where there is nothing.
当我这样做时HttpGet(url),我发现我的 url 中有一个非法字符(即 { 和 })。如果我在此之前对 URL 进行编码,则没有错误,但请求会转到一个没有任何内容的不同 URL。
The url, although has illegal characters, works from the browser but the HttpGetimplementation doesn't let me use it. What should I do or use instead to avoid this problem?
该 url 虽然包含非法字符,但可以在浏览器中使用,但HttpGet实现不允许我使用它。我应该做什么或使用什么来避免这个问题?
回答by Mike
http://java.sun.com/javase/6/docs/api/java/net/URLEncoder.html
http://java.sun.com/javase/6/docs/api/java/net/URLEncoder.html
Specifically:
具体来说:
String safeUrl = URLEncoder.encode("domain.com/service/{username}", "UTF-8");
回答by Chris
This question is old but it helped me and I just wanted to add for anyone who might come by that I fixed this issue on my app with a variation of Mike's reply.
这个问题很老,但它对我有帮助,我只是想为任何可能来的人补充,我在我的应用程序上修复了这个问题,并使用了 Mike 的回复。
String safeUrl = "domain.com/service/" + URLEncoder.encode("{username}", "UTF-8");
I found that encoding just the relevant parts worked, where attempting to encode the entire url caused an error for me.
我发现仅对相关部分进行编码有效,尝试对整个 url 进行编码会导致我出错。
回答by Arash
we shouldn't use URLEncoder.encode for address part of URL because it wrongly changes your http://domain.com/{username} to http%3A%2F%2Fdomain.com%2{username} and you should know it will replace all spaces with '+' that it was better to me to replace them with "%20".
我们不应该将 URLEncoder.encode 用于 URL 的地址部分,因为它错误地将您的http://domain.com/{username}更改为 http%3A%2F%2Fdomain.com%2{username} 并且您应该知道它会用“+”替换所有空格,对我来说最好用“%20”替换它们。
Here this function encodes only last part of your URL which is {username} or file name or anything that may have illegal characters.
此处此函数仅对 URL 的最后一部分进行编码,即 {username} 或文件名或任何可能包含非法字符的内容。
String safeUrl(String inUrl)
{
int fileInd = inUrl.lastIndexOf('/') + 1;
String addr = inUrl.substring(0, fileInd);
String fileName = inUrl.substring(fileInd);
String outUrl=null;
try {
outUrl = addr + URLEncoder.encode(fileName, "UTF-8");
outUrl = outUrl.replace("+", "%20");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return outUrl;
}

