Caused by: org.apache.http.ProtocolException: 没有指定目标主机,即使它没有格式错误
Caused by: org.apache.http.ProtocolException: Target host is not specified even though its not malformed
目前我正在围绕 github api 工作。
当尝试使用 Apache HTTP api 以我的身份验证令牌作为参数发送一个简单的 http GET 时,
控制台抛出这个:
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:107)
at xyz.lexium.giapb.http.GIAPBHttp.sendGet(GIAPBHttp.java:25)
at xyz.lexium.giapb.GIAPB.main(GIAPB.java:12)
Caused by: org.apache.http.ProtocolException: Target host is not specified
at org.apache.http.impl.conn.DefaultRoutePlanner.determineRoute(DefaultRoutePlanner.java:70)
at org.apache.http.impl.client.InternalHttpClient.determineRoute(InternalHttpClient.java:124)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:183)
... 4 more
这是我当前的代码:
package xyz.lexium.giapb.http;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class GIAPBHttp {
public static final String baseUrl = "https://api.github.com";
private static final String USER_AGENT = "Mozilla/5.0";
// HTTP GET request
public static void sendGet() throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("baseUrl");
CloseableHttpResponse response1 = httpclient.execute(httpGet);
try {
System.out.println(response1.getStatusLine());
HttpEntity entity1 = response1.getEntity();
// responce
EntityUtils.consume(entity1);
} finally {
response1.close();
}
HttpPost httpPost = new HttpPost(baseUrl);
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("access_token", "cant_steal_this_(dododododo)"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response2 = httpclient.execute(httpPost);
try {
System.out.println(response2.getStatusLine());
HttpEntity entity2 = response2.getEntity();
// responce
EntityUtils.consume(entity2);
} finally {
response2.close();
}
}
}
是的,我已经阅读了关于此的其他问题。但是我修改了它。这是一个完整的URL,所以其他的没有帮助
您正在尝试使用 httpclient 访问 HTTPS
资源。这导致了协议错误。
如此处所述,访问 https 网址需要做更多工作 -
第 25 行
HttpGet httpGet = new HttpGet("baseUrl");
应该是
HttpGet httpGet = new HttpGet(baseUrl);
目前我正在围绕 github api 工作。 当尝试使用 Apache HTTP api 以我的身份验证令牌作为参数发送一个简单的 http GET 时, 控制台抛出这个:
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:107)
at xyz.lexium.giapb.http.GIAPBHttp.sendGet(GIAPBHttp.java:25)
at xyz.lexium.giapb.GIAPB.main(GIAPB.java:12)
Caused by: org.apache.http.ProtocolException: Target host is not specified
at org.apache.http.impl.conn.DefaultRoutePlanner.determineRoute(DefaultRoutePlanner.java:70)
at org.apache.http.impl.client.InternalHttpClient.determineRoute(InternalHttpClient.java:124)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:183)
... 4 more
这是我当前的代码:
package xyz.lexium.giapb.http;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class GIAPBHttp {
public static final String baseUrl = "https://api.github.com";
private static final String USER_AGENT = "Mozilla/5.0";
// HTTP GET request
public static void sendGet() throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("baseUrl");
CloseableHttpResponse response1 = httpclient.execute(httpGet);
try {
System.out.println(response1.getStatusLine());
HttpEntity entity1 = response1.getEntity();
// responce
EntityUtils.consume(entity1);
} finally {
response1.close();
}
HttpPost httpPost = new HttpPost(baseUrl);
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("access_token", "cant_steal_this_(dododododo)"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response2 = httpclient.execute(httpPost);
try {
System.out.println(response2.getStatusLine());
HttpEntity entity2 = response2.getEntity();
// responce
EntityUtils.consume(entity2);
} finally {
response2.close();
}
}
}
是的,我已经阅读了关于此的其他问题。但是我修改了它。这是一个完整的URL,所以其他的没有帮助
您正在尝试使用 httpclient 访问 HTTPS
资源。这导致了协议错误。
如此处所述,访问 https 网址需要做更多工作 -
第 25 行
HttpGet httpGet = new HttpGet("baseUrl");
应该是
HttpGet httpGet = new HttpGet(baseUrl);