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;
        }
    }
    
    
  • 相关阅读:
    将绿色版Tomcat服务添加到系统服务并设为开机运行
    简单的递归遍历树
    js浏览器中的alert死浏览器
    Crontab文件的参数【转载】
    修改tomcat项目的图标
    最后两个and半月
    没有信的信乐团,依然让我动情
    The Network Adapter could not establish the connec
    MySql数据库的备份和恢复
    extjs
  • 原文地址:https://www.cnblogs.com/zhangbin1989/p/9772378.html
Copyright © 2011-2022 走看看