我正在尝试在我的一个参数中发出带冒号的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”
所以看起来冒号正在打破get请求。有没有办法来解决这个问题?为什么编码不解决问题?我的编码ID如下所示:
%7B%22ID%22%3A%22John%20Doe%22%7D
答案 0 :(得分:2)
当我运行您的代码的近似值时,生成的URL为:
https://127.0.0.0/getResourceNameToUse?id=%7B%22ID%22%3A%22John%20Doe%22%7D
就我所见,这是一个绝对有效的网址。我在其中看不到任何会混淆:
的{{1}}个字符。我们来看看例外情况:
HttpClient
我认为使用您编码的网址不的内容因为它显示java.net.UnknownHostException: 127.0.0.0/getResourceNameToUse?id={"ID"
而不是{"ID
。您的帖子中的代码是否有可能完全您正在运行的代码?
我还注意到你要去IP %7B%22ID%22
。您是否希望127.0.0.0
连接到localhost?
答案 1 :(得分:0)
我能够通过编码冒号的双重url来修复它:
String id = "{\"ID\":\"John Doe\"}";
id = id.replace(":","%3A");
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());