ClientProtocolException in httpClient.execute (httpget, responseHandler)

I use 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 calling httpclient.execute (httpget, responseHandler). The url works fine in a web browser, it returns xml and the browser displays it.

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

Change 1:

Looking at the protocol exception, a detailed message: "The server could not respond with a valid HTTP response." I cannot change the web server that I click on. Is there a way to ignore this and just access the answer?

Edit 2:

I found that the server is not sending the full header. Is there a way to access the contents of the response even if a damaged header is returned?

Edit 3: I edited the ip address to be the real IP address that I click on. Any help would be greatly appreciated.

+7
source share
6 answers

As your code seems to be correct, you should find out: is it a client error (invalid request) or server error (incorrect answer). To do this, use http trace utitlity and compare browser requests with your client's requests. You can also see the original response from the server, if any. If you can't figure it out, add a raw request and answer to your question, and someone can help.

+7
source

I checked your code with my IP address. There is no error in the code. I just changed the 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(); } 
+2
source

You can try adding your own interceptor response where you can add or remove headers or print some debugging information

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

Check your headers that you set in the client, re-check both the key and the value of the headers. In my case, I used the csrf parameter instead of the Cookie.

+1
source

I ran into a similar problem, ClientProtocolException
Obviously, the server did not have a target SSL web service
so it was the wrong SSL configuration,

The main error was:

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

After adding the store of trusted certificates in Java, this error disappeared.

hope this helps someone.

0
source

In my case, I removed the following dependency from the pom file, and this fixed the problem:

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

Source: https://habr.com/ru/post/1437324/


All Articles