zoukankan      html  css  js  c++  java
  • HttpUtils

    import java.io.IOException;
    import java.net.URLEncoder;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Set;
    
    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.ResponseHandler;
    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.entity.ContentType;
    import org.apache.http.entity.StringEntity;
    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;
    
    /**
     * 发送HTTP请求工具类
     * @author Rong.zhu
     *
     */
    public class HttpUtils {
        private final static int CONNECT_TIMEOUT_VALUE = 20000;//请求(连接)超时:20秒
        private final static int SOCKET_TIMEOUT_VALUE = 60000;//响应超时:60秒,一定要设置,否则可能导致当前线程一直处于等待状态
        
        private static RequestConfig requestConfig;
        private static ResponseHandler<String> responseHandler;
        
        static{
            requestConfig = RequestConfig.custom()
                    .setConnectTimeout(CONNECT_TIMEOUT_VALUE)//请求超时:connect timed out. connect refused
                    .setSocketTimeout(SOCKET_TIMEOUT_VALUE) //响应超时:response timed out 
                    .build();
            
            responseHandler = new ResponseHandler<String>() {
                public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
                    int status = response.getStatusLine().getStatusCode();
                    if (status >= 200 && status < 300) {
                        HttpEntity entity = response.getEntity();
                        return entity != null ? EntityUtils.toString(entity, "UTF-8") : null;
                    } else {
                        throw new ClientProtocolException("request failed,unexpected response status: " + status);
                    }
                }
            };
        }
        
        
        public static String postJSON(String url, Object sendData )throws Exception {
            HttpPost httppost = new HttpPost(url);
            httppost.setConfig(requestConfig);
            StringEntity entity = new StringEntity(JSONUtils.convertObject2Json(sendData),ContentType.create("application/json","UTF-8"));
            
            httppost.setEntity(entity);
            CloseableHttpClient httpClient = null;
            String responseBody = null;
            try{
                httpClient = HttpClients.createDefault();
                responseBody = httpClient.execute(httppost, responseHandler);
            }finally{
                if(httpClient != null) {
                    httpClient.close();
                }
            }
            return responseBody;
        }
        
        /**
         * 发送POST请求,该方法将会对参数进行UTF-8编码
         * @param url
         * @param params
         * @return
         * @throws Exception
         */
        public static String post(String url, Map<String, String> params) throws Exception {
            //封装请求参数
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            if(params != null) {
                Set<Entry<String, String>> entrySet = params.entrySet();
                for(Entry<String, String> entry : entrySet) {
                    nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
                }
            }
            //对参数进行转码(特殊字符)
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nvps, "UTF-8"); 
            HttpPost httppost = new HttpPost(url);
            httppost.setConfig(requestConfig);
            httppost.setEntity(entity);
            CloseableHttpClient httpClient = null;
            String responseBody = null;
            try{
                httpClient = HttpClients.createDefault();
                responseBody = httpClient.execute(httppost, responseHandler);
            }finally{
                if(httpClient != null) {
                    httpClient.close();
                }
            }
            return responseBody;
        }
        
        
        /**
         * 发送GET请求,该方法将会对参数进行UTF-8编码
         * @param url
         * @return
         * @throws Exception
         */
        public static String get(String url) throws Exception {
            StringBuffer encodedUrl = new StringBuffer();
            if(url.contains("?")) {
                encodedUrl.append(url.substring(0,url.lastIndexOf("?"))).append("?");
                String paramstr = url.substring(url.lastIndexOf("?")+1);
                String[] params = paramstr.split("&");
                for(String param: params) {
                    String pkey = param.substring(0,param.lastIndexOf("="));
                    String pvalue = param.substring(param.lastIndexOf("=")+1);
                    encodedUrl.append(pkey).append("=").append(URLEncoder.encode(pvalue,"UTF-8")).append("&");//UTF-8转码
                }
                String temp = encodedUrl.toString();
                temp.substring(0, temp.lastIndexOf("&"));
                encodedUrl = new StringBuffer(temp);
            }else {
                encodedUrl.append(url);
            }
            HttpGet httpGet = new HttpGet(encodedUrl.toString());
            CloseableHttpClient httpClient = null;
            String responseBody = null;
            try{
                httpClient = HttpClients.createDefault();
                responseBody = httpClient.execute(httpGet, responseHandler);
            }finally{
                if(httpClient != null) {
                    httpClient.close();
                }
            }
            return responseBody;
        }
    
    }
  • 相关阅读:
    乱七八糟想到什么记什么1
    ubuntu下 安装python module的步骤 分类: python Module 2013-10-12 13:58 624人阅读 评论(0) 收藏
    python中文编码转换 分类: python基础学习 python 小练习 2013-10-11 17:22 331人阅读 评论(0) 收藏
    from ....import导入其他路径的模块 分类: python基础学习 2013-10-11 15:13 315人阅读 评论(0) 收藏
    贪婪 vs 不贪婪 分类: 正则表达式 2013-10-09 15:00 290人阅读 评论(0) 收藏
    删除列表元素时需注意的问题 分类: python基础学习 python 小练习 2013-10-09 14:02 293人阅读 评论(0) 收藏
    #小练习 输出1到100的质数 分类: python 小练习 2013-10-08 17:45 319人阅读 评论(0) 收藏
    Linux下python升级至2.7步骤 分类: python基础学习 python下载 2013-09-29 11:24 4266人阅读 评论(2) 收藏
    实例、类、父类的关系判断 分类: python基础学习 2013-09-27 10:47 614人阅读 评论(0) 收藏
    #小练习 替换文件某行内容 分类: python 小练习 python Module 2013-09-26 11:10 269人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/7474568.html
Copyright © 2011-2022 走看看