Java org.apache.http.ProtocolException:未指定目标主机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24985771/
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
org.apache.http.ProtocolException: Target host is not specified
提问by ac11
I have written a simple httprequest/response code and I am getting this below error. I have referenced httpclient, httpcore, common-codecs and common-logging in my classpath. I am very new to java and have no clue what is going on here. Please help me.
我已经编写了一个简单的 httprequest/response 代码,但出现以下错误。我在我的类路径中引用了 httpclient、httpcore、common-codecs 和 common-logging。我对java很陌生,不知道这里发生了什么。请帮我。
Code:
代码:
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.HttpResponse;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.Header;
import org.apache.http.HttpHeaders;
public class UnshorteningUrl {
public static void main(String[] args) throws Exception
{
HttpGet request=null;
HttpClient client = HttpClientBuilder.create().build();
try {
request = new HttpGet("trib.me/1lBFzSi");
HttpResponse httpResponse=client.execute(request);
Header[] headers = httpResponse.getHeaders(HttpHeaders.LOCATION);
// Preconditions.checkState(headers.length == 1);
String newUrl = headers[0].getValue();
System.out.println("new url" + newUrl);
} catch (IllegalArgumentException e) {
// TODO: handle exception
}finally {
if (request != null) {
request.releaseConnection();
}
}
}}
Error:
错误:
Exception in thread "main" org.apache.http.client.ClientProtocolException
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
at UnshorteningUrl.main(UnshorteningUrl.java:26)
Caused by: org.apache.http.ProtocolException: Target host is not specified
at org.apache.http.impl.conn.DefaultRoutePlanner.determineRoute(DefaultRoutePlanner.java:69)
at org.apache.http.impl.client.InternalHttpClient.determineRoute(InternalHttpClient.java:124)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:183)
... 4 more
采纳答案by Sotirios Delimanolis
The error message is kind of misleading. You've provided a value that does not represent a complete URI
错误消息有点误导。您提供的值不代表完整的 URI
request = new HttpGet("trib.me/1lBFzSi");
It's missing a protocol.
它缺少一个协议。
Simply provide a complete URI
只需提供完整的 URI
request = new HttpGet("http://trib.me/1lBFzSi");
回答by Sam
This error is probably caused by the wrong url. Check the url:
此错误可能是由错误的 url 引起的。检查网址:
- protocal missing;
- url parameters encode;
- 协议缺失;
- url参数编码;
回答by angel.lopezrial
In my case I solvid adding / at the beggining: request = new HttpGet("/trib.me/1lBFzSi");
在我的情况下,我解决了在开始时添加 / 的问题: request = new HttpGet("/trib.me/1lBFzSi");