利用HttpClient来发送get与post请求

导入pom文件

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

		<dependency>
			<groupId>com.facebook.presto</groupId>
			<artifactId>presto-jdbc</artifactId>
			<version>0.152</version>
		</dependency>

开始发送请求

package com.example.demo.config;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.facebook.presto.jdbc.internal.guava.collect.Maps;

@SuppressWarnings("deprecation")
public class HttpUtils {

    private static final Logger log= LoggerFactory.getLogger(HttpUtils.class);

    public static String httpGetTool(List<NameValuePair> nvps, String urlPath, Map headerMap) {
        System.out.println(nvps + " " + urlPath + " " + headerMap);
        //HttpEntity entity = new UrlEncodedFormEntity(pairs,HTTP.UTF_8);
        long stime = System.currentTimeMillis();
        HttpClient http = new DefaultHttpClient();
        http.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,3600000);//连接时间
        http.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,3600000);//数据传输时间
        StringBuilder url = new StringBuilder(urlPath);
        StringBuffer sb = new StringBuffer();
        if (null != nvps && nvps.size() > 0) {
            url.append("?");
            for (int i = 0; i < nvps.size(); i++) {
                try {
                    String value = nvps.get(i).getValue();
                    if (value != null) {
                        url.append(nvps.get(i).getName()).append("=").append(URLEncoder.encode(value, "utf-8"))
                                .append("&");
                    }
                } catch (UnsupportedEncodingException e) {
                    log.error("{}", e);
                }
            }

            if (url.toString().endsWith("&")) {
                url.deleteCharAt(url.lastIndexOf("&"));
            }
        }
        HttpGet httpget = new HttpGet(url.toString());

        try {
            setHeader(httpget, headerMap);
            HttpResponse response = http.execute(httpget);
            HttpEntity entity = response.getEntity();
            // 显示结果
            BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "utf-8"));
            String line = null;

            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            if (reader != null) {
                reader.close();
            }
        } catch (UnsupportedEncodingException e) {
            httpget.abort();
            log.error("{}", e);
        } catch (ClientProtocolException e) {
            httpget.abort();
            log.error("{}", e);
        } catch (IOException e) {
            httpget.abort();
            log.error("{}", e);
        } finally {
            //log.info("|" + (System.currentTimeMillis() - stime) + ":ms|" + url.toString() + "|" + sb.toString());
        }
        return sb.toString();
    }


    public static JSONObject httpPostTool(List<NameValuePair> nvps, String urlPath, Map headerMap, int connection_timeout, int so_timeout) {
        long stime = System.currentTimeMillis();
        HttpClient http = new DefaultHttpClient();
        http.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connection_timeout);//连接时间
        http.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, so_timeout);//数据传输时间
        HttpPost httpPost = new HttpPost(urlPath);
        UrlEncodedFormEntity entityform;
        StringBuffer sb = new StringBuffer();
        String tmpParam = "";
        boolean f = false;
        try {
            entityform = new UrlEncodedFormEntity(nvps, HTTP.UTF_8);
            tmpParam = EntityUtils.toString(entityform);
            setHeader(httpPost, headerMap);
            httpPost.setEntity(entityform);
            HttpResponse response = http.execute(httpPost);

            long executetime = System.currentTimeMillis() - stime;

            if(executetime >= connection_timeout){
                f = true;
            }

            HttpEntity entity = response.getEntity();
            // 显示结果
            BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
            String line = null;

            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            if (reader != null) {
                reader.close();
            }
        } catch(Exception e) {
            log.error(urlPath,e);
        } finally {
            log.info("|" + (System.currentTimeMillis() - stime) + ":ms|" + urlPath + "|"
            );
        }
        LinkedHashMap<String, Object> root = Maps.newLinkedHashMap();

        try {
            if (sb != null) {
                root = JSONObject.parseObject(sb.toString(),new TypeReference<LinkedHashMap<String, Object>>(){}, Feature.OrderedField);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            log.error(sb.toString());
        }


        JSONObject jsonObject = new JSONObject(root);
        if(jsonObject != null && f){
            jsonObject.put("timeout", 1);
        }/* else if(jsonObject == null){
			jsonObject = new JSONObject();
		}*/
        return jsonObject;
    }

    public static String httpPostTool(List<NameValuePair> nvps, String urlPath, Map headerMap) {
        long stime = System.currentTimeMillis();
        HttpClient http = new DefaultHttpClient();
        http.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3600000);//连接时间
        http.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 3600000);//数据传输时间
        HttpPost httpPost = new HttpPost(urlPath);
        UrlEncodedFormEntity entityform;
        StringBuffer sb = new StringBuffer();
        String tmpParam = "";
        try {
            entityform = new UrlEncodedFormEntity(nvps, HTTP.UTF_8);
            tmpParam = EntityUtils.toString(entityform);
            setHeader(httpPost, headerMap);
            httpPost.setEntity(entityform);
            HttpResponse response = http.execute(httpPost);
            HttpEntity entity = response.getEntity();
            // 显示结果
            BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            if (reader != null) {
                reader.close();
            }
        } catch(Exception e) {
            log.error(urlPath,e);
        } finally {
            log.info("|" + (System.currentTimeMillis() - stime) + ":ms|" + urlPath + "|"
            );
        }
        return sb.toString();
    }

    public static String httpPostTool(Map<String, String> nvps, String urlPath, Map headerMap) {
        long stime = System.currentTimeMillis();
        HttpClient http = new DefaultHttpClient();
        http.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3600000);//连接时间
        http.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 3600000);//数据传输时间
        HttpPost httpPost = new HttpPost(urlPath);
        UrlEncodedFormEntity entityform;
        StringBuffer sb = new StringBuffer();
        try {
            List<NameValuePair> params = bulidParams(nvps);
            entityform = new UrlEncodedFormEntity(params, HTTP.UTF_8);
            setHeader(httpPost, headerMap);
            httpPost.setEntity(entityform);
            HttpResponse response = http.execute(httpPost);
            HttpEntity entity = response.getEntity();
            // 显示结果
            BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            if (reader != null) {
                reader.close();
            }
        } catch(Exception e) {
            log.error(urlPath,e);
        } finally {
            log.info("|" + (System.currentTimeMillis() - stime) + ":ms|" + urlPath + "|"
            );
        }
        return sb.toString();
    }

    public static void setHeader(HttpGet httpGet, Map headerMap) {

        if (headerMap != null) {
            Iterator iter = headerMap.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                String key = (String) entry.getKey();
                String val = (String) entry.getValue();
                httpGet.setHeader(key, val);
            }
        }
    }

    private static void setHeader(HttpPost httpPost, Map headerMap) {

        if (headerMap != null) {
            Iterator iter = headerMap.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                String key = (String) entry.getKey();
                String val = null;
                if (entry.getValue() != null) {
                    val = String.valueOf(entry.getValue());
                    httpPost.setHeader(key, val);
                }

            }
        }
    }

    public static HttpUriRequest getHttpGetRequest(String url, Map<String, String> headers) {
        RequestBuilder requestBuilder = RequestBuilder.get().setUri(url);
        if (headers != null) {
            for (Map.Entry<String, String> headerEntry : headers.entrySet()) {
                requestBuilder.addHeader(headerEntry.getKey(), headerEntry.getValue());
            }
        }
        RequestConfig.Builder requestConfigBuilder = RequestConfig.custom()
                .setConnectionRequestTimeout(20 * 1000).setSocketTimeout(20 * 1000)
                .setConnectTimeout(20 * 1000).setCookieSpec(CookieSpecs.BEST_MATCH);
        requestBuilder.setConfig(requestConfigBuilder.build());
        return requestBuilder.build();
    }

    private static List<NameValuePair> bulidParams(Map<String, String> map) throws UnsupportedEncodingException {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        for(Entry<String, String> entry : map.entrySet()) {
            params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
        return params;
    }

}

结果数据测试

        List<NameValuePair> pairs = new ArrayList<NameValuePair>();

        pairs.add(new BasicNameValuePair("sql",sql));
        pairs.add(new BasicNameValuePair("type","1"));
        pairs.add(new BasicNameValuePair("appid","admin_test"));
        pairs.add(new BasicNameValuePair("schema",schema));
        pairs.add(new BasicNameValuePair("catalog","hive"));
        pairs.add(new BasicNameValuePair("isQuaryAll","true"));

        Map<String, Object> maps = new HashMap<>();
        maps.put("Content-Type", "application/x-www-form-urlencoded");

        JSONObject jsonObject = HttpUtils.httpPostTool(pairs, url, maps, 30 * 30 * 1000, 30 * 30 * 1000);
        return jsonObject;

猜你喜欢

转载自blog.csdn.net/qq_38976693/article/details/85615440