Send a get request with httpcomponents, at least need:
CloseableHttpClient httpClient=HttpClients. CreateDefault ();
HttpGet HttpGet=new HttpGet (url);
//Create a custom response handler
ResponseHandlerResponseHandler=new ResponseHandler () {
Private HttpResponse response;
@ Override
Public String handleResponse (final HttpResponse response) throws IOException {
This. The response=response;
Int status=response. GetStatusLine (.) getStatusCode ();
If (the status & gt;=200 & amp; & The status & lt; 300) {
HttpEntity entity=response. GetEntity ();
Return the entity!=null? EntityUtils. ToString (entity, "utf-8") : null;
} else {
Throw new ClientProtocolException (" Unexpected response status: "+ status);
}
}
};
String responseBody;
Try {
ResponseBody=httpClient. Execute (httpGet responseHandler);
} the catch (IOException e) {
Throw new HttpUtilsException (" Get from "+ url +" error. ", e);
} the finally {
HttpClient. Close ();
}
Encapsulated with ZHTTP, now send a get request, only need the following steps:
BasicHttpUtils BasicHttpUtils=new BasicHttpUtils (String class, url);
String responseBody;
Try {
BasicHttpUtils responseBody=(String). The get ();
} the catch (HttpUtilsException e) {
Throw new ServiceException (" Get from "+ url +" error. ", e);
} the catch (IOException e) {
Throw new ServiceException (" Get from "+ url +" error. ", e);
}
ZHTTP has offered to send a post request, download files, send with Authorization Header of the HTTP request, with the form data in the request body, take the text or json data in the request body functions, such as
Details please click ZHTTP open source project address: https://gitee.com/zhu-yinli/zhttp