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 ...
随机推荐
- sql排序 去除默认升降序排序case方法////遍历数据库所有表及统计表数据总数
case排序法: end 还有EXEC法 可以网上查 SQLServer遍历数据库所有表及统计表数据总数: DECLARE @TableName varchar(); CREATE TABLE #Ge ...
- Android 静默安装
有时候我们需要软件实现静默安装,但是Android并未提供相应的API,然而我们知道命令行安装android的时候是不会提示用户的,所有要实现这个功能,我们就可以从执行命令行的方式实现.android ...
- Unity中HDR外发光的使用
1.使用标准材质,设置好Emission外发光 2.Camera下打开HDR,加上Bloom特效 最终效果 如果只勾选HDR,只会有高光感,不会有外发光 注意,正向光照下打开HDR不可用抗锯齿,否则切 ...
- Linux下缓冲区溢出攻击的原理及对策(转载)
前言 从逻辑上讲进程的堆栈是由多个堆栈帧构成的,其中每个堆栈帧都对应一个函数调用.当函数调用发生时,新的堆栈帧被压入堆栈:当函数返回时,相应的堆栈帧从堆栈中弹出.尽管堆栈帧结构的引入为在高级语言中实现 ...
- js调用MVC3自带js验证
验证: if ($(this).is("form")) { return $(this).validate().checkForm() ...
- 将具有关联关系的两个表从hibernate查询出来转成json对象时报错
第一篇文章: 相信大家做过JSON相关的东西对这个异常并不陌生,这个异常是由于JSONObject插件内部会无限拆解你传入的对象,直到没有可拆解为止,问题就在这,如果你传入的对象有外键关系,或者相互引 ...
- tensor
初始化 z = torch.Tensor(,,,,) --可以创建多维数组.里面是随机的数. s = torch.Tensor(,):fill() --用1填充 t = torch.rand(,) m ...
- Json 入门例子(form表单)【0】
<script> $(function () { var aa = $("#fm").serialize().replace(/\+/g, ""); ...
- 【MySQL】MySQL PLSQL Demo - 006.循环(WHILE DO and FOR LOOP)
WHILE DO drop procedure if exists p_while_do; create procedure p_while_do() begin declare i int; ; d ...
- VC++ 使用WebBrowser控件中html文件以资源形式加载
. . . . //加载资源文件中的HTML,IDR_HTML1就是HTML文件在资源文件中的ID wchar_t self_path[MAX_PATH] = { }; GetModuleFileNa ...