博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HttpURLConnection请求接口
阅读量:6433 次
发布时间:2019-06-23

本文共 4719 字,大约阅读时间需要 15 分钟。

 

 

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;    }

 

转载地址:http://qzxga.baihongyu.com/

你可能感兴趣的文章
东大OJ-1588: Routing Table
查看>>
CactiEZ 中文版V10.1安装使用以及139邮箱短信报警设置
查看>>
Xilinx发布新版SDAccel开发环境加速数据中心应用
查看>>
Oracle PLSQL Demo - 01.定义变量、打印信息
查看>>
把图片生成Base64字符串
查看>>
突破LVS瓶颈,LVS Cluster部署(OSPF + LVS) - lxcong的运维技术 - 开源中国社区
查看>>
花生壳宣布网站的网址直接绑定到详细的项目——jboss版本
查看>>
问题-[Delphi]在对GRID设置单击为编辑时,其他GRID可以,但有一个GRID不行?
查看>>
[Java IO]06_JSON操作
查看>>
转: 软件版本号的命名
查看>>
BZOJ 2588 Count on a tree (COT) 是持久的段树
查看>>
unity, unlit surface shader (texColor only surface shader)
查看>>
STL中的set容器的一点总结(转)
查看>>
在线最优化求解(Online Optimization)之三:FOBOS
查看>>
unity, sprite atlas
查看>>
[CareerCup] 4.8 Contain Tree 包含树
查看>>
HDU 4946 Area of Mushroom 凸包
查看>>
nginx 编译选项
查看>>
android TextView 带滚动条,和ScrollView 用法(暂时觉得ScrollView滑动速度比较快)...
查看>>
poj 3187 Backward Digit Sums(穷竭搜索dfs)
查看>>