zoukankan      html  css  js  c++  java
  • 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;
        }
  • 相关阅读:
    实验三-并发程序 20175201张驰
    20175201 20175215 20175229 实验二 固件程序设计
    20175201 20175215 20175229 实验一 开发环境的熟悉
    #20175201 实验五 网络编程与安全
    云班课选做
    2019-2020-12 20175313 20175328 20175329 实验五 通讯协议设计
    2019-2020-1 20175313 20175328 20175329 实验四 外设驱动程序设计
    2019-2020-1 20175313 20175328 20175329 实验三 并发程序
    20175329&20175313&20175318 2019-2020 《信息安全系统设计基础》实验三
    20175329&20175313&20175318 2019-2020 《信息安全系统设计基础》实验二
  • 原文地址:https://www.cnblogs.com/shihaiming/p/6170676.html
Copyright © 2011-2022 走看看