123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- package com.lightinit.hsdataportal.common;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import java.util.List;
- import java.util.Map;
- import org.apache.http.HttpEntity;
- import org.apache.http.NameValuePair;
- import org.apache.http.ParseException;
- import org.apache.http.StatusLine;
- import org.apache.http.client.ClientProtocolException;
- 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.entity.StringEntity;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.util.EntityUtils;
- import org.springframework.util.StringUtils;
- public class HttpTookitUtils {
- public static int getApplicationStatus(String url) {
- int statusCode=-1;
- CloseableHttpClient httpclient = HttpClients.createDefault();
- try {
- HttpGet httpget = new HttpGet(url);
- CloseableHttpResponse response = httpclient.execute(httpget);
- try {
- StatusLine statusLine = response.getStatusLine();
- statusCode= statusLine.getStatusCode();
- } finally {
- response.close();
- }
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (ParseException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- httpclient.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return statusCode;
- }
- public static String doGet(String url, List<NameValuePair> params, CharEncoding charset,String accept) {
- String responseContent="";
- CloseableHttpClient httpclient = HttpClients.createDefault();
- try {
- String queryParams="";
- if(params!= null) {
- for (NameValuePair pair : params) {
- if (queryParams.length() > 0) {
- queryParams += "&";
- }
- queryParams += pair.getName() + "=" + pair.getValue();
- }
- }
- if(StringUtils.isEmpty(queryParams)==false){
- if(url.contains("?")){
- url+="&";
- }else{
- url+="?";
- }
- url+=queryParams;
- }
- HttpGet httpget = new HttpGet(url);
- if(StringUtils.isEmpty(accept)==false)
- httpget.addHeader("Accept",accept);
- CloseableHttpResponse response = httpclient.execute(httpget);
- try {
- HttpEntity entity = response.getEntity();
- if (entity != null) {
- responseContent= EntityUtils.toString(entity, charset.value());
- }
- } finally {
- response.close();
- }
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (ParseException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- httpclient.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return responseContent;
- }
- public static String doGet(String url, List<NameValuePair> params, CharEncoding charset) {
- return doGet(url,params,charset,"");
- }
- public static String doPost(String url, List<NameValuePair> params, CharEncoding charset){
- return doPost(url,params,charset,"");
- }
- public static String doPost(String url, List<NameValuePair> params, CharEncoding charset,String accept) {
- String responseContent="";
- CloseableHttpClient httpclient = HttpClients.createDefault();
- HttpPost httppost = new HttpPost(url);
- if(StringUtils.isEmpty(accept)==false)
- httppost.addHeader("Accept",accept);
- UrlEncodedFormEntity uefEntity;
- try {
- uefEntity = new UrlEncodedFormEntity(params, charset.value());
- httppost.setEntity(uefEntity);
- CloseableHttpResponse response = httpclient.execute(httppost);
- try {
- HttpEntity entity = response.getEntity();
- if (entity != null) {
- responseContent=EntityUtils.toString(entity, charset.value());
- }
- } finally {
- response.close();
- }
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (UnsupportedEncodingException e1) {
- e1.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- httpclient.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return responseContent;
- }
- public static String doPost(String url, List<NameValuePair> params, CharEncoding charset, Map<String,String> headers) {
- String responseContent="";
- CloseableHttpClient httpclient = HttpClients.createDefault();
- HttpPost httppost = new HttpPost(url);
- if(headers!=null) {
- for (Map.Entry<String, String> entry : headers.entrySet()) {
- httppost.addHeader(entry.getKey(), entry.getValue());
- }
- }
- UrlEncodedFormEntity uefEntity;
- try {
- uefEntity = new UrlEncodedFormEntity(params, charset.value());
- httppost.setEntity(uefEntity);
- CloseableHttpResponse response = httpclient.execute(httppost);
- try {
- HttpEntity entity = response.getEntity();
- if (entity != null) {
- responseContent=EntityUtils.toString(entity, charset.value());
- }
- } finally {
- response.close();
- }
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (UnsupportedEncodingException e1) {
- e1.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- httpclient.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return responseContent;
- }
- public static String doPost(String url, String body, CharEncoding charset) {
- String responseContent="";
- CloseableHttpClient httpclient = HttpClients.createDefault();
- HttpPost httppost = new HttpPost(url);
- try {
- StringEntity uefEntity=new StringEntity(body, charset.value());
- httppost.setEntity(uefEntity);
- httppost.addHeader("Content-type","application/json");
- CloseableHttpResponse response = httpclient.execute(httppost);
- try {
- HttpEntity entity = response.getEntity();
- if (entity != null) {
- responseContent=EntityUtils.toString(entity, charset.value());
- }
- } finally {
- response.close();
- }
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (UnsupportedEncodingException e1) {
- e1.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- httpclient.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return responseContent;
- }
- public static String doPost(String url, String body, CharEncoding charset,Map<String,String> headers) {
- String responseContent="";
- CloseableHttpClient httpclient = HttpClients.createDefault();
- HttpPost httppost = new HttpPost(url);
- try {
- StringEntity uefEntity=new StringEntity(body, charset.value());
- httppost.setEntity(uefEntity);
- if(headers!=null) {
- for (Map.Entry<String, String> entry : headers.entrySet()) {
- httppost.addHeader(entry.getKey(), entry.getValue());
- }
- }else {
- httppost.addHeader("Content-type", "application/json");
- }
- CloseableHttpResponse response = httpclient.execute(httppost);
- try {
- HttpEntity entity = response.getEntity();
- if (entity != null) {
- responseContent=EntityUtils.toString(entity, charset.value());
- }
- } finally {
- response.close();
- }
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (UnsupportedEncodingException e1) {
- e1.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- httpclient.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return responseContent;
- }
- }
|