HttpClient get status code

天大地大妈咪最大 提交于 2019-12-01 02:39:30

EDIT:

Try with:

HttpResponse httpResp = client.execute(response);
int code = httpResp.getStatusLine().getStatusCode();

The HttpStatus should be 200 ( HttpStatus.SC_OK )

(I've read too fast the problem!)


Try with:

GetMethod getMethod = new GetMethod("http://www.example.com");
int res = client.executeMethod(getMethod);

This should do the trick!

How about this?

HttpResponse response = client.execute(getRequest);

// Status Code
int statusCode = response.getStatusLine().getStatusCode();

ResponseHandler<String> responseHandler = new BasicResponseHandler();
// Response Body
String responseBody = responseHandler.handleResponse(response);
anastluc

I do it like:

HttpResponse response = client.execute(httppost);
int status = response.getStatusLine().getStatusCode();

To get the respose body as a String though by not using a responseHandler I get it first as InputStream:

InputStream is = response.getEntity().getContent();

and then convert it to a String (ways how to do that here)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!