【发布时间】:2012-05-18 22:04:30
【问题描述】:
我正在尝试在我的一个参数中发出带有冒号的 get,但它以 unknownHostException 失败,这是我的代码:
String id = "{\"ID\":\"John Doe\"}";
String encodedID = URLEncoder.encode(id, "UTF-8").replace("+", "%20");
endpoint="https://127.0.0.1/getResourceNameToUse?id=" + encodedID;
HttpResponse response = new HttpResponse();
HttpGet httpget = new HttpGet(endpoint);
response = httpclient.execute(httpget, new RESTResponseHandler());
我收到以下错误:
java.net.UnknownHostException: 127.0.0.1/getResourceNameToUse?id={"ID"
因此,冒号似乎破坏了获取请求。有没有办法来解决这个问题?为什么编码它不能解决问题?我的编码 id 如下所示:
%7B%22ID%22%3A%22约翰%20Doe%22%7D【问题讨论】:
-
我能够通过对冒号进行双 url 编码来修复它: String id = id.replace(":","%3A");
标签: java httpclient url-encoding apache-httpclient-4.x