HttpURLConnection请求接口

 import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.commons.lang3.StringUtils;

import cn.zsmy.constant.BaseConstant;


/**
     * @param path
     * @param key
     * @param value
     * @return
     */
    public static String getHttp(String path, String key, String value){
        HttpURLConnection httpURLConnection = null;
        InputStream bis = null;
        ByteArrayOutputStream bos = null;
        try {
            URL url = new URL(path);
            //打开连接
            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestProperty("content-Type", "application/json");
            httpURLConnection.setRequestProperty("charset", "utf-8");
            if(key != null && value != null){
                httpURLConnection.setRequestProperty(key, value);
            }
            BaseConstant.MY_LOG.info("===getHttp==httpURLConnection.getResponseCode()===" + httpURLConnection.getResponseCode());
            if(200 == httpURLConnection.getResponseCode()){
                //得到输入流
                bis = httpURLConnection.getInputStream();
                bos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while(-1 != (len = bis.read(buffer))){
                    bos.write(buffer,0,len);
                    bos.flush();
                }
                return bos.toString("utf-8");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(httpURLConnection != null){
                httpURLConnection.disconnect();
            }
            if(bis != null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bos != null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }
    
    /**
     * POST请求获取数据
     */
    public static String postHttp(String path, String params){
        URL url = null;
        HttpURLConnection httpURLConnection = null;
        BufferedInputStream bis = null;
        ByteArrayOutputStream bos = null;
        try {
            url = new URL(path);
            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");// 提交模式
            httpURLConnection.setRequestProperty("content-Type", "application/json");
            //httpURLConnection.setRequestProperty("charset", "utf-8");
            // conn.setConnectTimeout(10000);//连接超时 单位毫秒
            // conn.setReadTimeout(2000);//读取超时 单位毫秒
            // 发送POST请求必须设置如下两行
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
            // 发送请求参数
            if(!StringUtils.isEmpty(params)){
                printWriter.write(params);//post的参数 xx=xx&yy=yy
            }
            // flush输出流的缓冲
            printWriter.flush();
            
            BaseConstant.MY_LOG.info("===postHttp==httpURLConnection.getResponseCode()===" + httpURLConnection.getResponseCode());
            
            //开始获取数据
            bis = new BufferedInputStream(httpURLConnection.getInputStream());
            bos = new ByteArrayOutputStream();
            int len;
            byte[] arr = new byte[1024];
            while((len=bis.read(arr))!= -1){
                bos.write(arr,0,len);
                bos.flush();
            }
            bos.close();
            return bos.toString(BaseConstant.CHARSET_UTF8);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(httpURLConnection != null){
                httpURLConnection.disconnect();
            }
            if(bis != null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bos != null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
原文地址:https://www.cnblogs.com/shihaiming/p/6170676.html