zoukankan      html  css  js  c++  java
  • HttpClient

    1、引入依赖

    <!-- httpclient -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.2</version>
    </dependency>
    

    2、写工具类

    package com.caad.clean.common.util;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.config.RequestConfig;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;
    
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    
    public class HttpClientUtil {
        private RequestConfig requestConfig = RequestConfig
                .custom()
                .setSocketTimeout(5000)
                .setConnectTimeout(5000)
                .setConnectionRequestTimeout(5000)
                .build();
    
        private static HttpClientUtil instance = new HttpClientUtil();
    
        private HttpClientUtil(){
        }
    
        public static HttpClientUtil getInstance(){
            return instance;
        }
    
        public String post(String url){
            HttpPost httpPost = new HttpPost(url);
            httpPost.setConfig(requestConfig);
            return httpPost(httpPost);
        }
    
        public String post(String url, Map<String, String> map){
            HttpPost httpPost = new HttpPost(url);
            httpPost.setConfig(requestConfig);
    
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            for (Map.Entry<String, String> entry: map.entrySet()) {
                nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
            }
            try {
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return httpPost(httpPost);
        }
    
        private String httpPost(HttpPost httpPost){
            CloseableHttpClient httpClient = null;
            CloseableHttpResponse response = null;
            String responseContent = "";
            try {
                httpClient = HttpClients.createDefault();
                response = httpClient.execute(httpPost);
                HttpEntity entity = response.getEntity();
                responseContent = EntityUtils.toString(entity, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if(response != null){
                        response.close();
                    }
                    if(httpClient != null){
                        httpClient.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return responseContent;
        }
    }
    
    
  • 相关阅读:
    分布式事务处理总结
    职业生涯的第一个三年
    Linux 启动SVN服务
    TensorFlow入门:安装常用的依赖模块
    TensorFlow入门:mac 安装 TensorFlow
    shiro 配置导图
    spring集成JPA的三种方法配置
    tomcat server 报错之 More than the maximum allowed number of cookies
    Jquery系列:checkbox 获取值、选中、设置值、事件监听等操作
    [转]Tomcat优化之内存、并发、缓存
  • 原文地址:https://www.cnblogs.com/zhangbin1989/p/9772378.html
Copyright © 2011-2022 走看看