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. myeclipse的快捷键和myeclipse快捷键设置

    1.Ctrl+M切换窗口的大小   2.Ctrl+Q跳到最后一次的编辑处   3.F2当鼠标放在一个标记处出现Tooltip时候按F2则把鼠标移开时Tooltip还会显示即Show Tooltip D ...

  2. StringComparison枚举

    public enum StringComparison { CurrentCulture, CurrentCultureIgnoreCase, InvariantCulture, Invariant ...

  3. HDU(2255),KM算法,最大权匹配

    题目链接 奔小康赚大钱 Time Limit: 1000/1000MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  4. SQL生成规则数

    --------------------------开始----------------------------开始值DECLARE @start INT = 1--结束值DECLARE @end I ...

  5. Mybaits 之根据集合查询和逗号分隔的子查询

    这是我们的mapper要根据传入一个集合进行查询: List<ExtKeywordCategory> findListByIds(List<ExtKeywordFkCategory& ...

  6. combotree的加载方法

    <!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...

  7. sql去除某个字段中的某个字符串 replace

    update A set col1 =REPLACE ( col1 ,'测试' , '') where col1 like '%测试%' 在使用过程中如果遇到text类型的字段时会报 参数数据类型 t ...

  8. SqlSever基础 intersect 交集 两个查询结果共有的

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  9. 建立exception包,编写TestException.java程序,主方法中有以下代码,确定其中可能出现的异常,进行捕获处理。

    package exception; public class TestException { public static void main(String[] args) { for(int i=0 ...

  10. 编写一个Java程序,计算一下1,2,…,9这9个数字可以组成多少个互不相同的、无重复数字的三位偶数。

    package a; public class SanWeiOuShu { public static void main(String[] args) { String str="1234 ...