• 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;
        }
    }
    
    
  • 相关阅读:
    751时尚公开课(三)宋杰林:时尚大跃进_豆瓣
    第九课堂-经验与技能分享交易网站
    751D·PARK北京时尚设计广场_百度百科
    2014马上有乐趣 每周六免费服装缝纫体验课_豆瓣
    设计工作室寻求合作
    【基础穿搭法】关于穿衣显高和好看的三个小技巧。(请深爱)
    给快播指一条生路:转型会员付费吧
    美华美依 | 创业谱
    服装配饰_MAVIN MARVY 高级服装定制_西服定制_衬衫定制_西装定制
    探索者系列_百度百科
  • 原文地址:https://www.cnblogs.com/zhangbin1989/p/9772378.html
Copyright © 2020-2023  润新知