我必须做什么来解决“java.lang.IllegalArgumentException”?
我正在尝试通过 HttpGet() 方法处理以下 URL:
我得到以下异常:
java.lang.IllegalArgumentException:
Invalid uri 'https://graph.facebook.com/search?q=Cafe++Bakery&type=event&access_token=&type=event&access_token=239090718395|lqqOnRWlcJOb3QGp3G4HW2aqhlc.': Invalid query
at org.apache.commons.httpclient.HttpMethodBase.<init>(HttpMethodBase.java:222)
at org.apache.commons.httpclient.methods.GetMethod.<init>(GetMethod.java:89)
现在当我剪切 &将该 URL 粘贴到浏览器中,它就可以正常工作了。我是 猜测是需要进行某种 URL 编码,但我不确定必须更改什么才能从 Http 客户端调用 url。
提前致谢。
I am trying to process the following URL via the HttpGet() method:
And I get the following exception:
java.lang.IllegalArgumentException:
Invalid uri 'https://graph.facebook.com/search?q=Cafe++Bakery&type=event&access_token=&type=event&access_token=239090718395|lqqOnRWlcJOb3QGp3G4HW2aqhlc.': Invalid query
at org.apache.commons.httpclient.HttpMethodBase.<init>(HttpMethodBase.java:222)
at org.apache.commons.httpclient.methods.GetMethod.<init>(GetMethod.java:89)
Now when I cut & paste that URL into the browswer it works just fine. I am
guess it is some sort of URL Encoding that needs to occur, but I am not sure what I have to change to call url from Http Client.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 URLEncoder.encode() 对 URL 进行编码
Use URLEncoder.encode() to encode the URL
根据 RFC 1738,您尝试连接的 URL 不是有效 URL。字符
'|'
不能以未编码的方式出现在 URL 中;参见第 2.2 节。使用
URLEncoder.encode()
不是答案。问题是URLEncoder.encode()
不是为此任务而设计的。相反,它设计用于将原始字符数据编码为“application/x-www-form-urlencoded”MIME 格式。这将:'/'
、':'
、'?'
等字符进行 % 编码麻烦的'|'
,'%'
字符进行编码...导致双重 %-encoding ,并将'+'< /code> 字符,导致 URL 损坏。
(请参阅 javadoc
UrlEncoder
了解对哪些字符进行编码以及如何编码的精确规范。)所有这些不正确/过度热心都可能是有害的,具体取决于 Web 服务器处理 URL 的方式。以安全的名义,许多网络服务器处理那些语法上重要的字符被不必要地编码的 URL,并且会重复解码,直到没有有效的 % 编码序列剩余。因此,在很多情况下,您可以不用使用
URLEncoder
。但是任何网络服务器都不应尝试将
'+'
字符转换为空格字符。有些防守技巧可能会出现问题;例如,如果您确实需要在 URL 中发送'%'
数据字符。那么真正的解决方案是什么呢?不幸的是这很难。正确的做法是使用能够容忍 URL 语法错误的解析器将 URL 解析为其组成部分,然后依靠 URL(或 URI)类将其重新组合在一起,以便根据需要正确地对组件进行编码通过 URL / URI 规范。
或者,拒绝该 URL。毕竟,它是无效的。
The URL you are trying to connect to is not a valid URL according to RFC 1738. The character
'|'
cannot appear unencoded in a URL; see section 2.2.Using
URLEncoder.encode()
is NOT the answer. The problem is thatURLEncoder.encode()
is not designed for this task. Rather it is designed for encoding raw character data to the "application/x-www-form-urlencoded" MIME format. This will:'/'
,':'
,'?'
and so on, in addition to the troublesome'|'
,'%'
characters ... resulting in double %-encoding , and'+'
characters, resulting in URL mangling.(Refer to the javadoc for
UrlEncoder
for a precise spec of what characters are encoded, and how.)All of these incorrect / over-zealous can be harmful, depending on how the web server handles the URLs. In the name of security, a lot of webservers cope with URLs where syntactically significant characters have been encoded unnecessarily, and will repeatedly decode until no valid-looking %-encoding sequences remain. So in a lot of cases, you can get away with using
URLEncoder
.But no webserver should attempt to turn
'+'
characters into space characters. And some of the defensive tricks can be problematical; e.g. if you really need to send a'%'
data character in the URL.So what is the real solution? Unfortunately it is difficult. The correct thing to do is to parse the URL into its constituent parts using a parser that is tolerant of URL syntax errors, and the put it back together relying on the URL (or URI) class to encode the components correctly as required by the URL / URI specifications.
Alternatively, reject the URL. After all, it is invalid.
我遇到了同样的问题,网址看起来很正常,没有可见的奇怪字符或任何东西。
解决了通过以下方式发送 URL 的问题:
maybeIn CorrectUrl
是有问题的 url。I had same problem, url looks normal as normal it can be , no visible strange characters or anything.
Solved prob with sending URL thru this:
maybeIncorrectUrl
beeing problematic url.