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;
        }
    }
    
    
  • 相关阅读:
    51Nod1740 蜂巢迷宫
    51Nod1279 扔盘子
    51Nod1095 Anigram单词
    51Nod1094 和为k的连续区间
    51Nod1072 威佐夫游戏
    PHP 图片处理
    ubuntu 安装 ftp
    linux下ab网站压力测试命令
    iptables FOr linux
    discuz 个性化时间函数
  • 原文地址:https://www.cnblogs.com/zhangbin1989/p/9772378.html
Copyright © 2011-2022 走看看