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;
}
HttpURLConnection请求接口的更多相关文章
- HttpURLConnection请求数据流的写入(write)和读取(read)
URLConnection类给应用 程序 和web资源之间架设起了通信的桥梁,这些web资源通常是通过url来标记的,本文将讲述如何使用HttpURLConnection来访问web页面(发送数据流) ...
- 项目二(业务GO)——跨域上传图片(请求接口)
之前,就听过“跨域上传”图片的问题,只是疏于研究,也就一再搁置,直至今天再次遇见这个不能避免的“坑”,才不得不思考一下,怎么“跨域上传”图片或者文件? 问题来源: 何为“跨域”? ——就是给你一个接口 ...
- iOS开发-网络-合理封装请求接口
概述 如今大多App都会与网络打交道,作为开发者,合理的对网络后台请求接口进行封装十分重要.本文要介绍的就是一种常见的采用回调函数(方法)的网络接口封装,也算的是一种构架吧. 这个构架主要的idea是 ...
- Android使用HttpUrlConnection请求服务器发送数据详解
HttpUrlConnection是java内置的api,在java.net包下,那么,它请求网络同样也有get请求和post请求两种方式.最常用的Http请求无非是get和post,get请求可以获 ...
- webServices 使用GET请求接口方法
webServices 若要使用GET请求接口方法在Web.config 下添加这段 <webServices> <protocols> <add ...
- 使用fiddler模拟重复请求接口
使用fiddler模拟重复请求接口 重复请求某个接口,比如评论一条,这样点击多次就可以造多个评论数据
- axios请求接口的踩坑之路
1.跨域问题除了前端安装插件还需要后端php设置,设置如下 Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, ...
- 一个vue请求接口渲染页面的例子
new Vue({ el:'#bodylist', data: { list: [ { "type_id": "1", "type_name" ...
- 使用ajax请求接口,跨域后cookie无法设置,全局配置ajax;及使用axios跨域后cookie无法设置,全局配置axios
问题一: 使用ajax/axios跨域请求接口,后端放行了,能够正常获取数据,但是cookie设置不进去,后端登录session判断失效 ajax解决办法: //设置ajax属性 crossDomai ...
随机推荐
- java.lang.VerifyError异常
以前遇到过java.lang.VerifyError 原因是jar包冲突 tomcat6自带jsp.jar.servlet.jar所以项目中不用引入 tomcat5不带jsp.jar.servlet. ...
- 周赛-DZY Loves Chessboard 分类: 比赛 搜索 2015-08-08 15:48 4人阅读 评论(0) 收藏
DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input standard ...
- SharePoint自动化系列——Manage "Site Subscriptions" using PowerShell
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 你可以将普通的sites加入到你的site subscriptions中,前提是你需要有一个 Te ...
- CssSpritesGenerator使用
最近一直在看CSS 的东西,在优化之路是哪个继续前进. 什么是css sprites css sprites直译过来就是CSS精灵,但是这种翻译显然是不够的,其实就是通过将多个图片融合到一副图里面,然 ...
- android 入门 003 (点击事件)
点击事件 有四种实现方式. 1.内部类实现方式 1.0 package cn.rfvip.clickevent; import android.app.Activity; import android ...
- CString.Format
Cstring str: str.Format("%d",num); d输出带符号十进制数 o输出无符号八进制数 x输出无符号十六进制数 u输出无符号数 c输出单个字符 s输出一串 ...
- VC 实现文件与应用程序关联(转载)
转载:http://www.cnblogs.com/RascallySnake/archive/2013/03/01/2939102.html 日常工作中,doc文件直接双击后,就能启动word软件, ...
- Python3基础 print 中字符串乘以数字,重复输出多次
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- HTML基础 整理
HTML:超文本传输协议 (Hyper Markup Language) CSS:网页美化 (Cascading Style Sheets) JS:java-scipt 脚本语言 Dreamweave ...
- nohup不输出日志信息的方法,及linux重定向学习
起因 最近使用nohup创建了一个后台进程,默认日志输出到了nohup.out文件中,程序跑起来也就没再管,过了大约一周,发现硬盘空间不够了,于是查找原因,发现这个nohup.out文件已经到了70G ...