1

Currently I am working around the github api. When trying to send a simple http GET with my auth token as a parameter using the Apache HTTP api, Console throws this:

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

Here is my current code:

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();
    }
}
}

YES, I have read the other issues on this. But i modified it. It is a full URL, so the other ones dont helpp

2 Answers2

5

Line 25

HttpGet httpGet = new HttpGet("baseUrl");

Should be

HttpGet httpGet = new HttpGet(baseUrl);
Luciano van der Veekens
  • 6,307
  • 4
  • 26
  • 30
bharti jindal
  • 61
  • 1
  • 3
1

You are trying to access an HTTPS resource using the httpclient. This is causing the protocol error.

Accessing https urls need some more work as explained here - Ignoring SSL certificate in Apache HttpClient 4.3

Community
  • 1
  • 1
Surojit
  • 1,282
  • 14
  • 26