引起:org.apache.http.ProtocolException:目标主机未指定即使其格式不正确

Mat*_*Jr. 1 java github apache-httpclient-4.x

目前我正在处理 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
Run Code Online (Sandbox Code Playgroud)

这是我当前的代码:

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();
    }
}
}
Run Code Online (Sandbox Code Playgroud)

是的,我已经阅读了关于此的其他问题。但我修改了它。这是一个完整的 URL,所以其他的没有帮助

小智 5

25号线

HttpGet httpGet = new HttpGet("baseUrl");
Run Code Online (Sandbox Code Playgroud)

应该

HttpGet httpGet = new HttpGet(baseUrl);
Run Code Online (Sandbox Code Playgroud)