http发送请求的工具类2

import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class GetAndPostUtil {
    /**
     * get方式发送请求
     *
     * @param url
     * @param params
     * @return
     * @throws Exception
     */
    public static String sendByGet(String url, String params) throws Exception {
        String retStr = "";

        // 创建url
        String tmpUrl = url;
        if (params != null) {
            tmpUrl += "?" + params;
        }
        URL urlObj = new URL(tmpUrl);

        // 打开http连接
        HttpURLConnection httpConn = (HttpURLConnection)(urlObj.openConnection());

        // 设置http连接属性
        httpConn.setConnectTimeout(30000);
        httpConn.setRequestMethod("GET");

        //设置跟踪Key
        httpConn.setRequestProperty("Track-Key", Thread.currentThread().getName());

        // 发送请求
        httpConn.connect();

        // 接受响应数据
        InputStream isr = httpConn.getInputStream();
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        int b;
        while ((b = isr.read()) != -1) {
            bao.write(b);
        }
        isr.close();

        // 关闭http连接
        httpConn.disconnect();

        // 返回响应数据,并设置为utf-8编码
        retStr = new String(bao.toByteArray(), "ISO-8859-1");
        retStr = new String(retStr.getBytes("ISO-8859-1"), "UTF-8");

        // retStr=new String(bao.toByteArray(), "UTF-8");

        return retStr;
    }

    /**
     * post方式发送请求
     *
     * @param url
     * @param params
     * @return
     * @throws Exception
     */
    public static String sendByPost(String url, String params) throws Exception {

        String retStr = "";

        if (StringUtils.isNotEmpty(url) && params != null) {

            // 创建url
            URL urlObj = new URL(url);

            // 打开http连接
            HttpURLConnection httpConn = (HttpURLConnection)(urlObj.openConnection());

            // 设置http连接属性
            httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            //设置跟踪Key
            httpConn.setRequestProperty("Track-Key", Thread.currentThread().getName());

            httpConn.setRequestMethod("POST");
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);

            // 发送请求数据,并设置为utf-8编码
            OutputStream out = httpConn.getOutputStream();
            out.write(params.getBytes("UTF-8"));
            out.close();

            // 接受响应数据
            InputStream isr = httpConn.getInputStream();
            ByteArrayOutputStream bao = new ByteArrayOutputStream();
            int b;
            while ((b = isr.read()) != -1) {
                bao.write(b);
            }
            isr.close();

            // 关闭http连接
            httpConn.disconnect();

            // 返回响应数据,并设置为utf-8编码
            retStr = new String(bao.toByteArray(), "ISO-8859-1");
            retStr = new String(retStr.getBytes("ISO-8859-1"), "UTF-8");

            // retStr=new String(bao.toByteArray(), "UTF-8");

        }
        return retStr;
    }

    /**
     * post方式发送请求
     *
     * @param url
     * @param params
     * @return
     * @throws Exception
     */
    public static String sendByPost(String url, String params,String contentType) throws Exception {

        String retStr = "";

        if (StringUtils.isNotEmpty(url) && params != null) {

            // 创建url
            URL urlObj = new URL(url);

            // 打开http连接
            HttpURLConnection httpConn = (HttpURLConnection)(urlObj.openConnection());

            // 设置http连接属性
            httpConn.setRequestProperty("Content-Type", contentType);
            //设置跟踪Key
            httpConn.setRequestProperty("Track-Key", Thread.currentThread().getName());

            httpConn.setRequestMethod("POST");
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);

            // 发送请求数据,并设置为utf-8编码
            OutputStream out = httpConn.getOutputStream();
            out.write(params.getBytes("UTF-8"));
            out.close();

            // 接受响应数据
            InputStream isr = httpConn.getInputStream();
            ByteArrayOutputStream bao = new ByteArrayOutputStream();
            int b;
            while ((b = isr.read()) != -1) {
                bao.write(b);
            }
            isr.close();

            // 关闭http连接
            httpConn.disconnect();

            // 返回响应数据,并设置为utf-8编码
            retStr = new String(bao.toByteArray(), "ISO-8859-1");
            retStr = new String(retStr.getBytes("ISO-8859-1"), "UTF-8");

            // retStr=new String(bao.toByteArray(), "UTF-8");

        }
        return retStr;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值