HTTP客户端工具

import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
/**
 * HTTP客户端工具
 *
 * @version 1.00
 * @date 2008/06/26
 */
public class HttpTool {
    /**
     * 默认编码UTF-8
     */
    private static String enc = "UTF-8";
    /**
     *
     * @param url URL
     * @param hm 哈希表
     * @param cookie 指定HTTP头Cookie字符串
     * @return 服务端响应字符串
     */
    public static HashMap postData(String url, HashMap hm, String cookie) {
        HashMap returnMap = new HashMap();
        HttpClient client = new HttpClient();
        PostMethod method = new UTF8PostMethod(url);
        try {
            if (cookie != null && cookie.length() > 0) {
                method.setRequestHeader("Cookie", cookie);
            }
            if (hm != null) {
                NameValuePair[] data = new NameValuePair[hm.size()];
                int i = 0;
                for (Object key : hm.keySet()) {
                    data[i++] = new NameValuePair(key.toString(), hm.get(key).toString());
                }
                method.setRequestBody(data);
            }
            client.executeMethod(method);
            if (cookie != null && cookie.length() > 0) {
                Header newCookieHeader = method.getResponseHeader("Set-Cookie");
                Header currentCookieHeader = method.getRequestHeader("Cookie");
                if (newCookieHeader != null) {
                    if (currentCookieHeader == null)
                        method.setRequestHeader("Cookie", newCookieHeader.getValue());
                    else
                        method.setRequestHeader("Cookie", currentCookieHeader.getValue() + "; " + newCookieHeader.getValue());
                }
            }
            returnMap.put("string", new String(method.getResponseBody(), enc));
            returnMap.put("cookie", method.getRequestHeader("Cookie").getValue());
        } catch (Exception e) {
            System.err.println("发生异常 原因: " + e.toString());
        } finally {
            method.releaseConnection();
        }
        return returnMap;
    }
    /**
     *
     * @param url URL
     * @param hm 哈希表
     * @return 服务端响应字符串
     */
    public static HashMap postData(String url, HashMap hm) {
        return postData(url, hm, "");
    }
    /**
     *
     * @param url URL
     * @param hm 哈希表
     * @param request HttpServletRequest对象
     * @return 服务端响应字符串
     */
    public static HashMap postData(String url, HashMap hm, HttpServletRequest request) {
        if (request != null) {
            return postData(url, hm, request.getHeader("cookie"));
        } else {
            return postData(url, hm);
        }
    }
    /**
     *
     * @param url URL
     * @param hm 哈希表
     * @param map 上次响应结果
     * @return 服务端响应字符串
     */
    public static HashMap postData(String url, HashMap hm, HashMap map) {
        if (map != null && map.get("cookie") != null) {
            return postData(url, hm, (String) map.get("cookie"));
        } else {
            return postData(url, hm);
        }
    }
    /**
     *
     * @param url URL
     * @param cookie 指定HTTP头Cookie字符串
     * @return 服务端响应字符串
     */
    public static HashMap getData(String url, String cookie) {
        HashMap returnMap = new HashMap();
        HttpClient client = new HttpClient();
        GetMethod method = new UTF8GetMethod(url);
        try {
            if (cookie != null && cookie.length() > 0) {
                method.setRequestHeader("Cookie", cookie);
            }
            client.executeMethod(method);
            if (cookie != null && cookie.length() > 0) {
                Header newCookieHeader = method.getResponseHeader("Set-Cookie");
                Header currentCookieHeader = method.getRequestHeader("Cookie");
                if (newCookieHeader != null) {
                    if (currentCookieHeader == null)
                        method.setRequestHeader("Cookie", newCookieHeader.getValue());
                    else
                        method.setRequestHeader("Cookie", currentCookieHeader.getValue() + "; " + newCookieHeader.getValue());
                }
            }
            returnMap.put("string", new String(method.getResponseBody(), enc));
            returnMap.put("cookie", method.getRequestHeader("Cookie").getValue());
        } catch (Exception e) {
            System.err.println("发生异常 原因: " + e.toString());
        } finally {
            method.releaseConnection();
        }
        return returnMap;
    }
    /**
     *
     * @param url URL
     * @return 服务端响应字符串
     */
    public static HashMap getData(String url) {
        return getData(url, "");
    }
    /**
     *
     * @param url URL
     * @param request HttpServletRequest对象
     * @return 服务端响应字符串
     */
    public static HashMap getData(String url, HttpServletRequest request) {
        if (request != null) {
            return getData(url, request.getHeader("cookie"));
        } else {
            return getData(url);
        }
    }
    /**
     *
     * @param url URL
     * @param map 上次响应结果
     * @return 服务端响应字符串
     */
    public static HashMap getData(String url, HashMap map) {
        if (map != null && map.get("cookie") != null) {
            return getData(url, (String) map.get("cookie"));
        } else {
            return getData(url);
        }
    }
    /**
     * Inner class for UTF-8 support
     */
    public static class UTF8PostMethod extends PostMethod {
        /**
         * @param url URL
         */
        public UTF8PostMethod(String url) {
            super(url);
        }
        /**
         * @return 默认编码
         */
        public String getRequestCharSet() {
            // return super.getRequestCharSet();
            return enc;
        }
    }
    /**
     * Inner class for UTF-8 support
     */
    public static class UTF8GetMethod extends GetMethod {
        /**
         * @param url URL
         */
        public UTF8GetMethod(String url) {
            super(url);
        }
        /**
         * @return 默认编码
         */
        public String getRequestCharSet() {
            // return super.getRequestCharSet();
            return enc;
        }
    }
}
  • 1
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值