Java MalformedURLException 虽然我已经用 %20 替换了空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24229660/
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
MalformedURLException although I have already replaced spaces with %20
提问by user3742241
String url = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins="+origin+"&destinations="+destination+"&mode=driving&sensor=false&language=en-EN&units=imperial";
url = url.replaceAll(" ", "%20");
Output :
输出 :
http://maps.googleapis.com/maps/api/distancematrix/xml?origins=150%20Sutter%20St%20San%20Francisco,%20CA,%20United%20States&destinations=1%20Palmer%20Sq%20E
Princeton,%20NJ%2008542&mode=driving&sensor=false&language=en-EN&units=imperial
But I am getting an error saying :
但我收到一条错误消息:
java.net.MalformedURLException: Illegal character in URL
Can some one help me out ..
有人可以帮我吗 ..
回答by Robby Cornelissen
(Note: see update below)
(注:见下方更新)
Use the URLEncoder
class from the java.net
package. Spaces are not the only characters that need to be escaped in URLs, and the URLEncoder
will make sure that all characters that need to be encoded are properly encoded.
使用包中的URLEncoder
类java.net
。空格并不是 URL 中唯一需要转义的字符,它URLEncoder
会确保所有需要编码的字符都被正确编码。
Here's a small example:
这是一个小例子:
String url = "http://...";
String encodedUrl = null;
try {
encodedUrl = URLEncoder.encode(url, "UTF-8");
} catch (UnsupportedEncodingException ignored) {
// Can be safely ignored because UTF-8 is always supported
}
Update
更新
As pointed out in the comments and other answers to this question, the URLEncoder
class is only safe to encode the query string parameters of a URL. I currently rely on Guava's UrlEscapers
to safely encode different parts of a URL.
正如对此问题的评论和其他答案所指出的那样,URLEncoder
该类仅对 URL 的查询字符串参数进行编码是安全的。我目前依靠 GuavaUrlEscapers
来安全地对 URL 的不同部分进行编码。
回答by M. Abbas
You shouldn't call String.replaceAll
to encode an URL, instead you should use java.net.URLEncoder.encode(String s, String enc)
.
您不应该调用String.replaceAll
对 URL 进行编码,而应该使用java.net.URLEncoder.encode(String s, String enc)
.
Note that you only need to encode the query string parameters (name and value) and not the entire URL.
请注意,您只需要对查询字符串参数(名称和值)进行编码,而不需要对整个 URL 进行编码。
When encoding a String using java.net.URLEncoder.encode(String s, String enc)
, the following rules apply:
使用 对字符串进行编码时java.net.URLEncoder.encode(String s, String enc)
,适用以下规则:
- The alphanumeric characters "a" through "z", "A" through "Z" and "0" through "9" remain the same.
- The special characters ".", "-", "*", and "_" remain the same.
- The space character " " is converted into a plus sign "+".
- All other characters are unsafe and are first converted into one
or more bytes using some encoding scheme. Then each byte is
represented by the 3-character string "%xy", where xy is the
two-digit hexadecimal representation of the byte. The recommended encoding scheme to use is UTF-8. However, for compatibility reasons,
if an encoding is not specified, then the default encoding of the
platform is used.
- 字母数字字符“a”到“z”、“A”到“Z”和“0”到“9”保持不变。
- 特殊字符“.”、“-”、“*”和“_”保持不变。
- 空格字符“”被转换为加号“+”。
- 所有其他字符都是不安全的,首先使用某种编码方案将其转换为一个或多个字节。然后每个字节由 3 个字符的字符串“%xy”表示,其中 xy 是字节的两位十六进制表示。推荐使用的编码方案是 UTF-8。但是,出于兼容性原因,如果未指定编码,
则使用平台的默认编码。
See URLEncoder
Ex:
前任:
String url = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins=" + URLEncoder.encode(origin, "UTF-8");
回答by To Kra
Here is the answer which helped me: HTTP URL Address Encoding in Java
这是对我有帮助的答案:Java 中的 HTTP URL 地址编码
Its just using URL
& URI
objects.
它只是使用URL
&URI
对象。
Original aproach mentioned above in this thread with URLEncoder
, encoded all characters in url including http://
and using such url
was throwing exception
in httpConnection
- therefore its not a good option. I am now using URL/URI
approach mentioned in link, and it's working as expected.
原始的形式给出了上面这个帖子中提到URLEncoder
,在URL编码的所有字符,包括http://
和使用这些url
被扔exception
在httpConnection
-所以它不是一个很好的选择。我现在正在使用URL/URI
链接中提到的方法,它按预期工作。
回答by Pregunton
java.net.URLEncoder.encode("Hello World", "UTF-8").replaceAll("\\+", "%20"));
have a nice day =).
java.net.URLEncoder.encode("Hello World", "UTF-8").replaceAll("\\+", "%20"));
有一个美好的一天=)。
回答by Sunit Mishra
Try
尝试
str.replaceAll("\s","%20");
That should fix it
那应该解决它