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

HttpURLConnection请求接口的更多相关文章

  1. HttpURLConnection请求数据流的写入(write)和读取(read)

    URLConnection类给应用 程序 和web资源之间架设起了通信的桥梁,这些web资源通常是通过url来标记的,本文将讲述如何使用HttpURLConnection来访问web页面(发送数据流) ...

  2. 项目二(业务GO)——跨域上传图片(请求接口)

    之前,就听过“跨域上传”图片的问题,只是疏于研究,也就一再搁置,直至今天再次遇见这个不能避免的“坑”,才不得不思考一下,怎么“跨域上传”图片或者文件? 问题来源: 何为“跨域”? ——就是给你一个接口 ...

  3. iOS开发-网络-合理封装请求接口

    概述 如今大多App都会与网络打交道,作为开发者,合理的对网络后台请求接口进行封装十分重要.本文要介绍的就是一种常见的采用回调函数(方法)的网络接口封装,也算的是一种构架吧. 这个构架主要的idea是 ...

  4. Android使用HttpUrlConnection请求服务器发送数据详解

    HttpUrlConnection是java内置的api,在java.net包下,那么,它请求网络同样也有get请求和post请求两种方式.最常用的Http请求无非是get和post,get请求可以获 ...

  5. webServices 使用GET请求接口方法

    webServices  若要使用GET请求接口方法在Web.config 下添加这段 <webServices>     <protocols>       <add  ...

  6. 使用fiddler模拟重复请求接口

    使用fiddler模拟重复请求接口 重复请求某个接口,比如评论一条,这样点击多次就可以造多个评论数据

  7. axios请求接口的踩坑之路

    1.跨域问题除了前端安装插件还需要后端php设置,设置如下 Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, ...

  8. 一个vue请求接口渲染页面的例子

    new Vue({ el:'#bodylist', data: { list: [ { "type_id": "1", "type_name" ...

  9. 使用ajax请求接口,跨域后cookie无法设置,全局配置ajax;及使用axios跨域后cookie无法设置,全局配置axios

    问题一: 使用ajax/axios跨域请求接口,后端放行了,能够正常获取数据,但是cookie设置不进去,后端登录session判断失效 ajax解决办法: //设置ajax属性 crossDomai ...

随机推荐

  1. HDU 4712:Hamming Distance

    Hamming Distance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  2. SQL Server 索引和表体系结构(二)

    转自:http://www.cnblogs.com/chenmh 非聚集索引 概述 对于非聚集索引,涉及的信息要比聚集索引更多一些,由于整个篇幅比较大涉及接下来的要写的“包含列的索引”,“索引碎片”等 ...

  3. Linux学习之八——利用变量

    一.变量的使用 用$放在变量前面进行使用,例如: echo $PATH 为了和别的字符隔开,可以用{}和"",例如 echo ${PATH}nic echo "$PATH ...

  4. NotificationObject.cs

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...

  5. Cow Bowling

    Cow Bowling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15585 Accepted: 10363 Descrip ...

  6. 基于@AspectJ和schema的aop(三)---切点函数详解

    切点函数是AspectJ表达式语言的核心, 也是使用@AspectJ进行切面定义的难点.本小节我们通过具体的实例对切点函数进行深入学习. 1.@annotation() @annotation()表示 ...

  7. 客服端调用自定义宿主的WCF报错"没有终结点在侦听可以接受消息的http://localhost:8085/mex。这通常是由于不正确的地址或者 SOAP 操作导致的错误"的解决方案。

    没有终结点在侦听可以接受消息的http://localhost:8085/mex.这通常是由于不正确的地址或者 SOAP 操作导致的错误. 这个错误是由于没有启动元数据交换终结点(MEX)导致的.在宿 ...

  8. Android内存Activity泄露:Threads

    Android编程中一个共同的困难就是协调Activity的生命周期和长时间运行的任务(task),并且要避免可能的内存泄露.思考下面Activity的代码,在它启动的时候开启一个线程并循环执行任务. ...

  9. linux硬盘分区格式化及挂载

    1.硬盘的接口类型 硬盘的接口一般分为两种,一种是IDE并行接口,一种是SATA串行接口, 在linux上面IDE接口的硬盘被识别为/dev/hd[a-z]这样的设备,其中hdc表示光驱设备,这是因为 ...

  10. SQL Server 存储过程(转载)

    SQL Server 存储过程 Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这 ...