Kevin Westwood
Kevin Westwood

Reputation: 7789

ClientProtocolException in httpClient.execute(httpget, responseHandler)

I am using the following code to request xml from a web server:

HttpClient httpclient = new DefaultHttpClient()
try 
{
    HttpGet httpget = new HttpGet("http://63.255.173.242/get_public_tbl.cgi?A=1");              
    ResponseHandler responseHandler = new BasicResponseHandler();
    String responseBody = httpclient.execute(httpget, responseHandler);
    System.out.println(responseBody);
}
catch (ClientProtocolException e)
{
    e.printStackTrace();
}
catch (IOException e)
{
    e.printStackTrace();
} 
finally 
{
    httpclient.getConnectionManager().shutdown();
}

I get a clientProtocolException when I call httpclient.execute(httpget, responseHandler). The url works just fine in a web browser, it returns the xml and the browser displays it.

Any ideas why I would get a clientProtocolException and yet the browser handles it just fine?

Edit 1:

Looking at the protocol exception the detail message is: "The server failed to respond with a valid HTTP response". I cannot change the web server that I am hitting. Is there a way to ignore this and just access the response?

Edit 2:

I have found that the server is not sending back a complete header. Is there a way to access the contents of the response even when a broken header is returned?

Edit 3: I edited the ip address to be the real IP address I am hitting. Any help would be much appreciated.

Upvotes: 7

Views: 63252

Answers (6)

Madhu Stv
Madhu Stv

Reputation: 89

In my case, I have removed the below dependency from the pom file and it fixed the issue:

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.9</version>
    </dependency>

Upvotes: -2

JavaSheriff
JavaSheriff

Reputation: 7675

I was facing a similar issue, ClientProtocolException
Apparently the server didn't have the target web service SSL
so it was wrong SSL configuration,

Underlying error was:

  java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty

After adding the truststore to java this error disappeared.

hope it help someone.

Upvotes: 0

Rahul Jain
Rahul Jain

Reputation: 276

Check your headers which you set in the client, re-verify both key and value of headers. In my case I was using "csrf" param instead of "Cookie".

Upvotes: 1

Evgeniy Berezovsky
Evgeniy Berezovsky

Reputation: 19228

As your code seems to be correct, you have to figure out: Is this the client's fault (invalid request), or the server's fault (invalid response). To do that, use a http trace utitlity and compare the requests of a browser to that of your client. You'll also be able to see the raw response from the server, if there is any. If you can't figure it out then, add the raw request and response to your question and someone might be able to help.

Upvotes: 8

lanwen
lanwen

Reputation: 2299

You could try to add your own response interceptor, where you can add or delete headers, or print some debug info

hc.addResponseInterceptor(new HttpResponseInterceptor() {
    @Override
    public void process(HttpResponse response, HttpContext context) throws HttpException, IOException {
        response.getEntity().writeTo(System.out);
    }
});

Upvotes: 2

Ram kiran Pachigolla
Ram kiran Pachigolla

Reputation: 21191

I have tested your code with my ip address. There is no error in code. I just changed ResponseHandler to BasicResponseHandler.

check this

 HttpClient httpclient = new DefaultHttpClient();
    try 
    {
        HttpGet httpget = new HttpGet("http://www.MyServer.com/get_public_tbl.cgi?A=1");               
        BasicResponseHandler responseHandler = new BasicResponseHandler();//Here is the change
        String responseBody = httpclient.execute(httpget, responseHandler);
        System.out.println(responseBody);
    }
    catch (ClientProtocolException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    } 
    finally 
    {
        httpclient.getConnectionManager().shutdown();
    }

Upvotes: 2

Related Questions