总结几个最近处理问题中使用http协议的代码
正文前先来一波福利推荐:
福利一:
百万年薪架构师视频,该视频可以学到很多东西,是本人花钱买的VIP课程,学习消化了一年,为了支持一下女朋友公众号也方便大家学习,共享给大家。
福利二:
毕业答辩以及工作上各种答辩,平时积累了不少精品PPT,现在共享给大家,大大小小加起来有几千套,总有适合你的一款,很多是网上是下载不到。
获取方式:
微信关注 精品3分钟 ,id为 jingpin3mins,关注后回复 百万年薪架构师 ,精品收藏PPT 获取云盘链接,谢谢大家支持!
------------------------正文开始---------------------------
demo1:几个不同的http请求方式总结:
-------------------------------------------------------------------------------------------------
Post新版本的请求方式:
基于的版本:
<!--<!– https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient –>-->
<!--<dependency>-->
<!--<groupId>org.apache.httpcomponents</groupId>-->
<!--<artifactId>httpclient</artifactId>-->
<!--<version>4.5.</version>-->
<!--</dependency>-->
// String uploadResponse = Request.Post(nodeAddress + "/" + bucketName + "/" + objectName)
// .addHeader("x-nos-token", uploadToken)
// .bodyByteArray(chunkData, 0, readLen)
// .execute()
// .returnContent()
// .toString();
-------------------------------------------------------------------------------------------------
Post请求版本方式二:
// public static String doPost(String url, String token, byte[] chunk, int off, int len)
// {
// CloseableHttpClient httpClient = null;
// CloseableHttpResponse httpResponse = null;
// String result = "";
// // 创建httpClient实例
// httpClient = HttpClients.createDefault();
// // 创建httpPost远程连接实例
// HttpPost httpPost = new HttpPost(url);
// // 配置请求参数实例
// RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 设置连接主机服务超时时间
// .setConnectionRequestTimeout(35000)// 设置连接请求超时时间
// .setSocketTimeout(60000)// 设置读取数据连接超时时间
// .build();
// // 为httpPost实例设置配置
// httpPost.setConfig(requestConfig);
// // 设置请求头
// httpPost.addHeader("x-nos-token", token);
// // 封装post请求参数
// // 为httpPost设置封装好的请求参数
// HttpEntity requestEntity = new ByteArrayEntity(chunk, 0, len);
// httpPost.setEntity(requestEntity);
//
// try {
// // httpClient对象执行post请求,并返回响应参数对象
// httpResponse = httpClient.execute(httpPost);
// // 从响应对象中获取响应内容
// HttpEntity entity = httpResponse.getEntity();
// result = EntityUtils.toString(entity);
// } catch (ClientProtocolException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// } finally {
// // 关闭资源
// if (null != httpResponse) {
// try {
// httpResponse.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// if (null != httpClient) {
// try {
// httpClient.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
// return result;
// }
-------------------------------------------------------------------------------------------------
Post请求版本方式二:
public static String sendPost(String url, String token, byte[] chunk, int off, int len)
{
// 创建httpClient实例对象
HttpClient httpClient = new HttpClient();
// 设置httpClient连接主机服务器超时时间:15000毫秒
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout();
// 创建post请求方法实例对象
PostMethod postMethod = new PostMethod(url);
// 设置post请求超时时间
postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, );
postMethod.addRequestHeader("x-nos-token", token);
try {
//json格式的参数解析 RequestEntity entity = new ByteArrayRequestEntity(chunk);
postMethod.setRequestEntity(entity); httpClient.executeMethod(postMethod);
String result = postMethod.getResponseBodyAsString();
postMethod.releaseConnection();
return result;
} catch (IOException e) {
logger.info("POST请求发出失败!!!");
e.printStackTrace();
}
return null;
}
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
GET请求方式一:
public static String doGet(String httpurl)
{
HttpURLConnection connection = null;
InputStream is = null;
BufferedReader br = null;
// 返回结果字符串
String result = null; try {
// 创建远程url连接对象
URL url = new URL(httpurl);
// 通过远程url连接对象打开一个连接,强转成httpURLConnection类
connection = (HttpURLConnection) url.openConnection();
// 设置连接方式:get
connection.setRequestMethod("GET");
// 设置连接主机服务器的超时时间:15000毫秒
connection.setConnectTimeout();
// 设置读取远程返回的数据时间:60000毫秒
connection.setReadTimeout();
// 发送请求
connection.connect();
// 通过connection连接,获取输入流
if (connection.getResponseCode() == ) {
is = connection.getInputStream();
// 封装输入流is,并指定字符集
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
// 存放数据
StringBuffer sbf = new StringBuffer();
String temp = null;
while ((temp = br.readLine()) != null) {
sbf.append(temp);
sbf.append("\r\n");
}
result = sbf.toString();
}
} catch (MalformedURLException e) {
logger.error("非法地址格式异常...");
e.printStackTrace();
} catch (IOException e) {
logger.error("Get请求中IO流出现异常");
e.printStackTrace();
} finally {
// 关闭资源
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
} if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
} connection.disconnect();// 关闭远程连接
} return result;
}
-------------------------------------------------------------------------------------------------
过程问题记录:
在使用get请求的时候,如果URL中的请求参数包含了特殊字符,需要对特殊字符进行转义:
有些字符在URL中具有特殊含义,基本编码规则如下:
特殊含义 十六进制值
.+ 表示空格(在 URL 中不能使用空格) %
./ 分隔目录和子目录 %2F
.? 分隔实际的 URL 和参数 %3F
.% 指定特殊字符 %
.# 表示书签 %
.& URL 中指定的参数间的分隔符 %
解决方法:
//对鉴权参数做AES加密处理
requestId = java.net.URLEncoder.encode( AESCryptUtils.encode(requestId) );
data = java.net.URLEncoder.encode( AESCryptUtils.encode(data) );
action = java.net.URLEncoder.encode( AESCryptUtils.encode(action) );
String URL = AUTH_URL + "?" + "data=" + data + "&requestId=" + requestId + "&action=" + action;
总结几个最近处理问题中使用http协议的代码的更多相关文章
- 1.JAVA中使用JNI调用C++代码学习笔记
Java 之JNI编程1.什么是JNI? JNI:(Java Natibe Inetrface)缩写. 2.为什么要学习JNI? Java 是跨平台的语言,但是在有些时候仍然是有需要调用本地代码 ( ...
- 当C#中带有return的TryCatch代码遇到Finally时代码执行顺序
编写的代码最怕出现的情况是运行中有错误出现,但是无法定位错误代码位置.综合<C#4.0图解教程>,总结如下: TryCatchFinally用到的最多的是TryCatch,Catch可以把 ...
- 微信公众平台中添加qq在线聊天代码
微信公众平台是个不错的媒体,可以和你的小伙伴们即时交流,但你的小伙伴们是用手机上的微信,打字自然就慢了:有人说用微信网页版,那个也不习惯,再说也不一定所有人都知道网页版微信.(2014.01.22更新 ...
- 在PHP与HTML混合输入的页面或者模板中就需要对PHP代码进行闭合
PHP程序的时候会在文件的最后加上一个闭合标签,如下: <?phpclass MyClass{public function test(){//do something, etc.}}?> ...
- 判断字符串中是否有SQL攻击代码
判断一个输入框中是否有SQL攻击代码 public const string SQLSTR2 = @"exec|cast|convert|set|insert|select|delete|u ...
- Android 中多点触摸协议
http://blog.csdn.net/zuosifengli/article/details/7398661 Android 中多点触摸协议: 参考: http://www.kernel.org/ ...
- C# WCF学习笔记(二)终结点地址与WCF寻址(Endpoint Address and WCF Addressing) WCF中的传输协议
URI的全称是 Uniform Rosource Identifire(统一资源标识),它唯一标识一个确定的网绐资源,同时也表示资源所处的位置及访问的方式(资源访问所用的网络协议). 对于Endpoi ...
- 网页中插入Flvplayer视频播放器代码
http://blog.csdn.net/china_skag/article/details/7424019 原地址:http://yuweiqiang.blog.163.com/blog/stat ...
- PHP中检测ajax请求的代码例子
多数情况下,基于JavaScript 的Js框架如jquery.Mootools.Prototype等,在发出Ajax请求指令时,都会发送额外的 HTTP_X_REQUESTED_WITH 头部信息, ...
随机推荐
- springboot项目上传文件大小限制问题
1.报错信息: Error parsing HTTP request header Note: further occurrences of HTTP header parsing errors wi ...
- Educational Codeforces Round 68
目录 Contest Info Solutions A.Remove a Progression B.Yet Another Crosses Problem C.From S To T D.1-2-K ...
- The Preliminary Contest for ICPC China Nanchang National Invitational
目录 Contest Info Solutions A. PERFECT NUMBER PROBLEM D. Match Stick Game G. tsy's number H. Coloring ...
- 贾扬清牛人(zz)
贾扬清加入阿里巴巴后,能否诞生出他的第三个世界级杰作? 文 / 华商韬略 张凌云 本文转载,著作权归原作者所有 贾扬清加入阿里巴巴后,能否诞生出他的第三个世界级杰作? 2017年1月11日,美国 ...
- [bzoj 5143][Ynoi 2018]五彩斑斓的世界
传送门 Descroption 给了你一个长为n的序列a,有m次操作 1.把区间[l,r]中大于x的数减去x 2.查询区间[l,r]中x的出现次数 Solution 分块 对于每个块,我们都分别维护: ...
- 浅谈Min_25筛(一看就懂的那种)
作用 前提:一个积性函数\(F(i)\),要求\(F(P^k),P\in prime\)可以快速计算 实现\(O(\frac{n^{\frac{3}{4}}}{logn})\):\(\sum\limi ...
- docker install and minikube install
1.选择国内的云服务商,这里选择阿里云为例 curl -sSL http://acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-engine/ ...
- [Ubuntu] A start job is running for...interfaces
CPU:RK3288 系统:Linux 移植 Ubuntu 16.04 到嵌入式平台,如果以太网有问题,在这里会耗时大约5分钟 开机后可以修改 Ubuntu 配置来缩短时间 打开下面的文件,可以看到最 ...
- 【源码】openresty 限流
小结: 1.在连接环节计数,有清零环节 有3个参量 maxburst unit_delay https://github.com/openresty/lua-resty-limit-traffic/b ...
- Web前端笔记整理
不使用Ajax无刷新提交: header('HTTP/1.1 204 No Content'); var a=document.createElement('img'); a.setAttribute ...