工具类【Http请求】

来源:互联网 发布:snmptrap协议端口号 编辑:程序博客网 时间:2024/05/18 20:08

声明:本文纯属个人随手笔记,如果对您有参考价值我十分开心,如果有存在错误,或者有更好的解决办法也麻烦您留言告诉我,大家共同成长,切勿恶言相。 欢迎加入资源共享QQ群:275343679,一起发现知识、了解知识、学习知识、分享知识。

个人网站www.itlantian.top  www.blueskyui.cn

=================================================================================

import java.io.IOException;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * http工具类
 * 
 * @Class Name HttpUtil
 * 
 * @author terence
 */
public class HttpUtil {

private static final Logger LOGGER = LoggerFactory.getLogger(HttpUtil.class);

    /**
     * 发送post请求工具方法
     * 
     * @Methods Name HttpPost
     * @param url
     * @param method
     * @param paramMap
     * @return String
     */
    @SuppressWarnings("rawtypes")
    public static String HttpPost(String url, String method, Map paramMap) {
        String encoding = "UTF-8";
        String webUrl = url + "/" + method;
        if (encoding == null || "".equals(encoding))
            encoding = "UTF-8";
        StringBuffer sBuffer = new StringBuffer();
        // 构造HttpClient的实例
        HttpClient httpClient = new HttpClient();
        // httpClient.set
        // 创建POS方法的实例
        NameValuePair[] pairs = null;
        PostMethod postMethod = new PostMethod(webUrl);
        if (paramMap != null) {
            pairs = new NameValuePair[paramMap.size()];
            Set set = paramMap.keySet();
            Iterator it = set.iterator();
            int i = 0;
            while (it.hasNext()) {
                Object key = it.next();
                Object value = paramMap.get(key);
                if(!HttpUtil.checkNull(value)){
                pairs[i] = new NameValuePair(key.toString(), value.toString());
                }
                i++;
            }
            postMethod.setRequestBody(pairs);
        }
        postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, encoding);
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000); // 连接5秒超时
        httpClient.getHttpConnectionManager().getParams().setSoTimeout(30000);// 读取30秒超时
        try {
            // 执行getMethod
            int statusCode = httpClient.executeMethod(postMethod);
            if (statusCode != HttpStatus.SC_OK) {
                System.err.println("Method failed: " + postMethod.getStatusLine());
                sBuffer = new StringBuffer();
            } else {
                sBuffer = new StringBuffer(postMethod.getResponseBodyAsString() + "");
            }
        } catch (HttpException e) {
            // 发生致命的异常,可能是协议不对或者返回的内容有问题
        LOGGER.debug("Please check your provided http address!");
        LOGGER.error("exception occured: {}", e.getMessage());
        } catch (IOException e) {
            // 发生网络异常
        LOGGER.error("exception occured: {}", e.getMessage());
        } finally {
            // 释放连接
            postMethod.releaseConnection();
        }
        return sBuffer.toString();
    }
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static String HttpGet(String webUrl, Map paramMap) {
    
    //设置编码格式
        String encoding = "GBK";
        if (encoding == null || "".equals(encoding))
            encoding = "GBK";
        String queryString = createLinkString(paramMap);
        webUrl = webUrl+"?"+queryString;
        StringBuffer sBuffer = new StringBuffer();
        // 构造HttpClient的实例
        HttpClient httpClient = new HttpClient();
        GetMethod gettMethod = null;  
        // httpClient.set
        try {
        URI uri = new URI(webUrl,false,encoding);
 
        gettMethod = new GetMethod(uri.toString());

        gettMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, encoding);
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000); // 连接5秒超时
        httpClient.getHttpConnectionManager().getParams().setSoTimeout(30000);// 读取30秒超时

            // 执行getMethod
            int statusCode = httpClient.executeMethod(gettMethod);
            if (statusCode != HttpStatus.SC_OK) {
                System.err.println("Method failed: " + gettMethod.getStatusLine());
                sBuffer = new StringBuffer();
            } else {
                sBuffer = new StringBuffer(gettMethod.getResponseBodyAsString() + "");
            }
        } catch (HttpException e) {
            // 发生致命的异常,可能是协议不对或者返回的内容有问题
        LOGGER.debug("Please check your provided http address!");
        LOGGER.error("exception occured: {}", e.getMessage());
        } catch (IOException e) {
            // 发生网络异常
        LOGGER.error("exception occured: {}", e.getMessage());
        } finally {
            // 释放连接
        gettMethod.releaseConnection();
        }
        return sBuffer.toString();
    }
    /**
     * 发送Get请求工具方法
     * @Methods Name HttpGet
     * @param url
     * @param method
     * @param paramMap
     * @return String
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static String HttpGet(String url, String method, Map paramMap) {

    String webUrl = url + "/" + method;
     
    return HttpGet(webUrl, paramMap);
    }
    
    /**
     * 发送Get请求工具方法,处理参数有中文字符
     * @Methods Name HttpGet
     * @param url
     * @param method
     * @param paramMap
     * @return String
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static String HttpGetByUtf(String url, String method, Map paramMap) {
    //设置编码格式
        String encoding = "UTF-8";
        String webUrl = url + "/" + method;
        if (encoding == null || "".equals(encoding))
            encoding = "UTF-8";
        String queryString = createLinkString(paramMap);
        webUrl = webUrl+"?"+queryString;
        StringBuffer sBuffer = new StringBuffer();
        // 构造HttpClient的实例
        HttpClient httpClient = new HttpClient();
        GetMethod gettMethod = null;  
        // httpClient.set
        try {
        URI uri = new URI(webUrl,false,encoding);
        gettMethod = new GetMethod(uri.toString());
        gettMethod.setRequestHeader("Content-type", "application/json");
        gettMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, encoding);
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000); // 连接5秒超时
        httpClient.getHttpConnectionManager().getParams().setSoTimeout(30000);// 读取30秒超时

            // 执行getMethod
            int statusCode = httpClient.executeMethod(gettMethod);
            if (statusCode != HttpStatus.SC_OK) {
                System.err.println("Method failed: " + gettMethod.getStatusLine());
                sBuffer = new StringBuffer();
            } else {
                sBuffer = new StringBuffer(gettMethod.getResponseBodyAsString() + "");
            }
        } catch (HttpException e) {
            // 发生致命的异常,可能是协议不对或者返回的内容有问题
        LOGGER.debug("Please check your provided http address!");
        LOGGER.error("exception occured: {}", e.getMessage());
        } catch (IOException e) {
            // 发生网络异常
        LOGGER.error("exception occured: {}", e.getMessage());
        } finally {
            // 释放连接
        gettMethod.releaseConnection();
        }
        return sBuffer.toString();
    }
    
    /**
     * 执行一个HTTP POST请求,返回请求响应的HTML
     * @param url
     * 请求的URL地址
     * @param params
     * 请求的查询参数,可以为null
     * @return 返回请求响应的HTML
     */
    @SuppressWarnings("deprecation")
    public static String doPost(String url, String json) {
        String response = null;
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(url);
        method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
        // 设置Http Post数据       
        try {
            method.setRequestBody(json); 
            method.setRequestHeader("Content-type", "application/json");
            client.executeMethod(method);
            //if (method.getStatusCode() == HttpStatus.SC_OK) {
                response = method.getResponseBodyAsString();
            //}
        } catch (IOException e) {
        LOGGER.error("exception occured: {}", e.getMessage());
        } finally {
            method.releaseConnection();
        }
        return response;
    }
    
    /**
     * 执行一个HTTP POST请求,无返回值
     * @param url
     * 请求的URL地址
     * @param params
     * 请求的查询参数,可以为null
     * @return 返回请求响应的HTML
     */
    @SuppressWarnings("deprecation")
    public static void doPostNoReturn(String url, String json) {
    
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(url);
        method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
        // 设置Http Post数据       
        try {
            method.setRequestBody(json); 
            method.setRequestHeader("Content-type", "application/json");
            client.executeMethod(method);
        } catch (IOException e) {
        LOGGER.error("exception occured: {}", e.getMessage());
        } finally {
            method.releaseConnection();
        }
    }
    
    /** 
     * 把数组所有元素排序,并按照“参数=参数值”的模式用“&”字符拼接成字符串
     * @param params 需要排序并参与字符拼接的参数组
     * @return 拼接后字符串
     */
    public static String createLinkString(Map<String, String> params) {

        List<String> keys = new ArrayList<String>(params.keySet());
        Collections.sort(keys);

        String prestr = "";

        for (int i = 0; i < keys.size(); i++) {

            String key = keys.get(i);
            String value = params.get(key);

            if (i == keys.size() - 1) {//拼接时,不包括最后一个&字符
                prestr = prestr + key + "=" + value;
            } else {
                prestr = prestr + key + "=" + value + "&";
            }
        }

        return prestr;
    }
    
    public static boolean checkNull(Object target) {
        if (target == null || "".equals(target.toString().trim()) || "null".equalsIgnoreCase(target.toString().trim())) {
            return true;
        }
        return false;
    }
}

原创粉丝点击