Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 原因:org.apache.http.ProtocolException:未指定目标主机,即使其格式不正确_Java_Github_Apache Httpclient 4.x - Fatal编程技术网

Java 原因:org.apache.http.ProtocolException:未指定目标主机,即使其格式不正确

Java 原因:org.apache.http.ProtocolException:未指定目标主机,即使其格式不正确,java,github,apache-httpclient-4.x,Java,Github,Apache Httpclient 4.x,目前,我正在围绕GithubAPI工作。 当尝试使用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 o

目前,我正在围绕GithubAPI工作。 当尝试使用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();
    }
}
}
包xyz.lexium.giapb.http;
导入java.util.ArrayList;
导入java.util.List;
导入org.apache.http.HttpEntity;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.CloseableHttpResponse;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.CloseableHttpClient;
导入org.apache.http.impl.client.HttpClients;
导入org.apache.http.message.BasicNameValuePair;
导入org.apache.http.util.EntityUtils;
公共类GIAPBHttp{
公共静态最终字符串baseUrl=”https://api.github.com";
私有静态最终字符串用户_AGENT=“Mozilla/5.0”;
//HTTP获取请求
公共静态void sendGet()引发异常{
CloseableHttpClient httpclient=HttpClients.createDefault();
HttpGet-HttpGet=newhttpget(“baseUrl”);
CloseableHttpResponse response1=httpclient.execute(httpGet);
试一试{
System.out.println(response1.getStatusLine());
HttpEntity entity1=response1.getEntity();
//回应
EntityUtils.consume(entity1);
}最后{
响应1.close();
}
HttpPost HttpPost=新的HttpPost(baseUrl);
List-nvps=newarraylist();
添加(新的BasicNameValuePair(“访问令牌”,“不能偷这个(DoDoDoDoDoDo)”);
setEntity(新的UrlEncodedFormEntity(nvps));
CloseableHttpResponse response2=httpclient.execute(httpPost);
试一试{
System.out.println(response2.getStatusLine());
HttpEntity entity2=response2.getEntity();
//回应
EntityUtils.consume(entity2);
}最后{
response2.close();
}
}
}

是的,我已经阅读了关于这方面的其他问题。但是我修改了它。这是一个完整的URL,因此其他URL无法帮助p

您正在尝试使用httpclient访问
HTTPS
资源。这导致协议错误

访问https URL需要更多的工作,如下所述-

您正试图使用httpclient访问
HTTPS
资源。这导致协议错误

访问https URL需要更多的工作,如下所述- 第25行

HttpGet httpGet = new HttpGet("baseUrl");
HttpGet httpGet = new HttpGet(baseUrl);
应该是

第25行

HttpGet httpGet = new HttpGet("baseUrl");
HttpGet httpGet = new HttpGet(baseUrl);
应该是