我有这样的代码:
URL url = new URL("http://foo.com/?param=paj%E9");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
...
但是,似乎 openConnection 正在抑制 url 的“%E9”部分,并且服务器最终收到请求 http://foo.com?param=paj
我是否忘记应用任何不同的设置才能使其正常工作?
谢谢!
编辑: url“http://foo.com/?param=paj%E9”已经编码(来自 http:// /foo.com/?param=pajé),这应该是服务器应该接收的请求。如果我尝试直接访问 http://foo.com/?param=paj%E9从浏览器来看,它按预期工作。如果我对“paj%E9”进行 URLEncode,我将对参数进行双重编码,并且服务器在解码该值时会看到“paj%E9”而不是“pajé”。
我实际上正在尝试构建一个代理,因此我收到了已经编码的网址。问题是,每当我使用 HttpURLConnection 传递要请求的此类编码参数时,它都会忽略编码部分(如 %E9)。
I have a code like this:
URL url = new URL("http://foo.com/?param=paj%E9");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
...
However, it seems like the openConnection is supressing the "%E9" part of the url, and the server ends up receiving a request http://foo.com?param=paj
Am I forgetting to apply any different setting for this to work properly?
Thanks!
EDIT: The url "http://foo.com/?param=paj%E9" is already encoded (from http://foo.com/?param=pajé), and this should be the request the server should receive. If I try to access http://foo.com/?param=paj%E9 straight from the browser, it works as expected. If I URLEncode "paj%E9", I'll be double-encoding the parameter, and the server would see "paj%E9" instead "pajé" upon decoding the value.
I'm actually trying to build a proxy, and therefore I receive the urls already encoded. The problem is that whenever I pass such an encoded parameter to be requested using HttpURLConnection, it simply ignores the encoded part (like %E9).
发布评论
评论(2)
您需要使用 java.net.URI 类来编码您的 URL,而不是您自己处理它。检查这个:
Java 中的 HTTP URL 地址编码
You need to use java.net.URI class to encode your URL instead of handle it on your own. Chek this:
HTTP URL Address Encoding in Java
您可以使用以下代码
URLEncoder.encode("中文", "utf-8")
You can use the following code
URLEncoder.encode("中文", "utf-8")