java 发送HTTP请求工具

package xx.xx.xx;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;

/**
 * 发送http请求工具
 *
 * @author Shi Zezhu
 * @date 2016年10月24日 下午5:01:06
 */
public class ConnectionUtils {
    /**
     * 请求类型:GET
     */
    public static final String HTTP_TYPE_GET = "GET";
    /**
     * 请求类型:POST
     */
    public static final String HTTP_TYPE_POST = "POST";

    /**
     * 发送http请求
     *
     * @param urlString
     *            url地址
     * @param method
     *            请求类型
     * @param parameters
     *            请求参数
     * @return JSONObject string
     * @throws IOException
     */
    private static String send(String urlString, String method, String data) throws IOException {
        if (StringUtils.isBlank(urlString) || StringUtils.isBlank(method)) {
            return null;
        }
        if (HTTP_TYPE_GET.equalsIgnoreCase(method) && StringUtils.isNotBlank(data)) {
            urlString = urlString + "?" + data;
        }
        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod(method);// 设置请求方法
        connection.setDoInput(true);// 设置是否使用输入流
        connection.setConnectTimeout(5 * 1000);
        connection.setReadTimeout(5 * 1000);
        
        if (HTTP_TYPE_POST.equalsIgnoreCase(method) && StringUtils.isNotBlank(data)) {
            connection.setDoOutput(true);// 设置是否使用输出流
            OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
            out.write(data);
            out.flush();
            out.close();
        }
        InputStream is = null;
        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK || connection.getResponseCode() == HttpURLConnection.HTTP_CREATED || connection.getResponseCode() == HttpURLConnection.HTTP_ACCEPTED) {
            is = connection.getInputStream();
        } else {
            is = connection.getErrorStream();
        }
        if (is == null) {
            return "";
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        String line = null;
        StringBuilder sb = new StringBuilder();
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        connection.disconnect();
        return sb.toString();
    }

    public static String mapToParamStr(Map<String, String> parameters) {
        String data = "";
        if (parameters != null && parameters.size() > 0) {
            int i = 0;
            StringBuilder sb = new StringBuilder();
            for (Entry<String, String> entry : parameters.entrySet()) {
                if (i > 0) {
                    sb.append("&");
                }
                sb.append(entry.getKey()).append("=").append(entry.getValue());
                i++;
            }
            data = sb.toString();
        }
        return data;
    }

    /**
     * 发送get请求
     *
     * @param urlString
     * @param parameters
     * @return
     * @throws IOException
     */
    public static String sendGet(String url, Map<String, String> parameters) throws IOException {
        if (StringUtils.isBlank(url)) {
            return null;
        }
        return send(url, HTTP_TYPE_GET, mapToParamStr(parameters));
    }
    
    /**
     * 发送get请求
     *
     * @param urlString
     * @param paramStr
     * @return
     * @throws IOException
     */
    public static String sendGet(String url, String paramStr) throws IOException {
        if (StringUtils.isBlank(url)) {
            return null;
        }
        return send(url, HTTP_TYPE_GET, paramStr);
    }

    /**
     * 发送get请求
     *
     * @param urlString
     * @return
     * @throws IOException
     */
    public static String sendGet(String url) throws IOException {
        if (StringUtils.isBlank(url)) {
            return null;
        }
        return send(url, HTTP_TYPE_GET, null);
    }

    /**
     * 发送post请求
     *
     * @param urlString
     * @param parameters
     * @return
     * @throws IOException
     */
    public static String sendPost(String url, Map<String, String> parameters) throws IOException {
        if (StringUtils.isBlank(url)) {
            return null;
        }
        return send(url, HTTP_TYPE_POST, mapToParamStr(parameters));
    }
    
    /**
     * 发送post请求
     *
     * @param urlString
     * @param paramStr
     * @return
     * @throws IOException
     */
    public static String sendPost(String url, String paramStr) throws IOException {
        if (StringUtils.isBlank(url)) {
            return null;
        }
        return send(url, HTTP_TYPE_POST, paramStr);
    }

    /**
     * 发送post请求
     *
     * @param urlString
     * @return
     * @throws IOException
     */
    public static String sendPost(String url) throws IOException {
        if (StringUtils.isBlank(url)) {
            return null;
        }
        return send(url, HTTP_TYPE_POST, null);
    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值