HttpUtils请求工具类
package com.cmcc.hybj.payment.framework.https;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
/**
* @author gy
*/
public class HttpsClientUtil {
private static final Logger LOG = LoggerFactory.getLogger(HttpsClientUtil.class);
/**
* 生成PostMethod
*
* @param url
* @return
*/
public static PostMethod generatePostMethod(String url) {
ProtocolSocketFactory fcty = new MySecureProtocolSocketFactory();
Protocol.registerProtocol("https", new Protocol("https", fcty, 443));
final PostMethod postMethod = new PostMethod(url);
return postMethod;
}
/**
* 生成PutMethod
*
* @param url
* @return
*/
public static PutMethod generatePutMethod(String url) {
ProtocolSocketFactory fcty = new MySecureProtocolSocketFactory();
Protocol.registerProtocol("https", new Protocol("https", fcty, 443));
final PutMethod putMethod = new PutMethod(url);
return putMethod;
}
/**
* 对访问url进行Base64编码
*
* @param url
* @return
*/
public static String enCodeUrlToBase64(String url) {
try {
if (url != null) {
String encodedUrl = new String(Base64.encodeBase64(url.getBytes()), "UTF-8");
return encodedUrl;
}
} catch (UnsupportedEncodingException e) {
LOG.error(e.getMessage());
e.printStackTrace();
}
return null;
}
/**
* 解析标准的POST响应
*
* @param client
* @param postMethod
* @param reqJson
*/
public static <T> T resloveDefaultResp(final HttpClient client, PostMethod postMethod,
String reqJson, Class<T> respClazz) {
try {
RequestEntity entity = new StringRequestEntity(reqJson, "application/json", "UTF-8");
postMethod.setRequestEntity(entity);
int executeCode = client.executeMethod(postMethod);
LOG.info("executeCode is :" + executeCode);
String result = postMethod.getResponseBodyAsString();
if (executeCode == 200) {
LOG.info("Execute Resp:" + JSON.toJSONString(result));
} else {
LOG.error(result);
}
T t = JSON.parseObject(result, respClazz);
return t;
} catch (Exception e) {
LOG.error(e.getMessage());
e.printStackTrace();
}
return null;
}
/**
* 解析标准的POST响应
*
* @param client
* @param postMethod
* @param reqJson
* @param respClazz
*/
public static String resloveDefaultRespList(String url, String json) {
String charset = null;
// // 创建默认的httpClient实例.
CloseableHttpClient client = HttpClientBuilder.create()
.setRedirectStrategy(new LaxRedirectStrategy()).build();
HttpPost post = new HttpPost(url);
try {
StringEntity s = new StringEntity(json);
s.setContentEncoding("UTF-8");
s.setContentType("application/json");
post.setEntity(s);
HttpResponse res = client.execute(post);
if (res.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = res.getEntity();
charset = EntityUtils.toString(entity);
LOG.info("返回参数" + charset);
return charset;
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return charset;
}
/**
* 解析标准Put的响应
*
* @param client
* @param putMethod
* @param reqJson
*/
public static <T> List<T> resloveDefaultPutRespList(final HttpClient client,
PutMethod putMethod, String reqJson,
Class<T> respClazz) {
try {
RequestEntity entity = new StringRequestEntity(reqJson, "application/json", "UTF-8");
putMethod.setRequestEntity(entity);
int executeCode = client.executeMethod(putMethod);
LOG.info("executeCode is :" + executeCode);
String result = putMethod.getResponseBodyAsString();
if (executeCode == 200) {
LOG.info("Execute Resp:" + result);
} else {
LOG.error(result);
}
List<T> t = JSONObject.parseArray(result, respClazz);
return t;
} catch (Exception e) {
LOG.error(e.getMessage());
e.printStackTrace();
}
return null;
}
/**
* 解析标准Put的响应
*
* @param client
* @param putMethod
* @param reqJson
*/
public static <T> T resloveDefaultPutResp(final HttpClient client, PutMethod putMethod,
String reqJson, Class<T> respClazz) {
try {
RequestEntity entity = new StringRequestEntity(reqJson, "application/json", "UTF-8");
putMethod.setRequestEntity(entity);
int executeCode = client.executeMethod(putMethod);
LOG.info("executeCode is :" + executeCode);
String result = putMethod.getResponseBodyAsString();
if (executeCode == 200) {
LOG.info("Execute Resp:" + result);
} else {
LOG.error(result);
}
T t = JSON.parseObject(result, respClazz);
return t;
} catch (Exception e) {
LOG.error(e.getMessage());
e.printStackTrace();
}
return null;
}
/**
* 解析标准Delete的响应
*
* @param client
* @param delMethod
* @param respClazz
*/
public static <T> T resloveDefaultDelResp(final HttpClient client, DeleteMethod delMethod,
Class<T> respClazz) {
try {
delMethod.setRequestHeader("content-type", "application/json;charset=UTF-8");
delMethod.setRequestHeader("content-encoding", "UTF-8");
int executeCode = client.executeMethod(delMethod);
LOG.info("executeCode is :" + executeCode);
String result = delMethod.getResponseBodyAsString();
if (executeCode == 200) {
LOG.info("Execute Resp:" + result);
} else {
LOG.error(result);
}
T t = JSON.parseObject(result, respClazz);
return t;
} catch (Exception e) {
LOG.error(e.getMessage());
e.printStackTrace();
}
return null;
}
/**
* HttpClient发送POST请求
*
* @param client
* @param postMethod
* @param nameValuePairs post请求的body [new NameValuePair(k,v),new NameValuePair(k,v)]
* @param respClazz
* @param <T>
* @return
*/
public static <T> T resloveDefaultResp(final HttpClient client, PostMethod postMethod,
NameValuePair[] nameValuePairs, Class<T> respClazz) {
try {
postMethod.setRequestBody(nameValuePairs);
int executeCode = client.executeMethod(postMethod);
LOG.info("executeCode is :" + executeCode);
String result = postMethod.getResponseBodyAsString();
if (executeCode == 200) {
LOG.info("Execute Resp:" + result);
} else {
LOG.error(result);
}
T t = JSON.parseObject(result, respClazz);
return t;
} catch (Exception e) {
LOG.error(e.getMessage());
e.printStackTrace();
}
return null;
}
}
HttpUtils请求工具类的更多相关文章
- Http请求工具类(Java原生Form+Json)
package com.tzx.cc.common.constant.util; import java.io.IOException; import java.io.InputStream; imp ...
- java jdk原生的http请求工具类
package com.base; import java.io.IOException; import java.io.InputStream; import java.io.InputStream ...
- WebUtils-网络请求工具类
网络请求工具类,大幅代码借鉴aplipay. using System; using System.Collections.Generic; using System.IO; using System ...
- Http、Https请求工具类
最近在做微信开发,使用http调用第三方服务API,有些是需要https协议,通过资料和自己编码,写了个支持http和https的工具类,经验证可用,现贴出来保留,也供需要的人使用(有不足的地方,也请 ...
- 微信https请求工具类
工作中用到的微信https请求工具类. package com.gxgrh.wechat.tools; import com.gxgrh.wechat.wechatapi.service.System ...
- HTTP请求工具类
HTTP请求工具类,适用于微信服务器请求,可以自测 代码; /// <summary> /// HTTP请求工具类 /// </summary> public class Ht ...
- 实现一个简单的http请求工具类
OC自带的http请求用起来不直观,asihttprequest库又太大了,依赖也多,下面实现一个简单的http请求工具类 四个文件源码大致如下,还有优化空间 MYHttpRequest.h(类定义, ...
- 远程Get,Post请求工具类
1.远程请求工具类 import java.io.*; import java.net.URL; import java.net.URLConnection; import java.util.L ...
- C#实现的UDP收发请求工具类实例
本文实例讲述了C#实现的UDP收发请求工具类.分享给大家供大家参考,具体如下: 初始化: ListeningPort = int.Parse(ConfigurationManager.AppSetti ...
随机推荐
- Chrome插件中 popup,background,contentscript消息传递机制
https://blog.csdn.net/summerxiachen/article/details/78698878 popup不能接收到contentscript 发送的消息, 如果这时cont ...
- Checking Types Against the Real World in TypeScript
转自:https://www.olioapps.com/blog/checking-types-real-world-typescript/ This is a follow-up to Type-D ...
- md5-js加密
JS-MD5加密/html页面使用 大家都知道,传输明文信息很不安全,尤其像密码.卡号等这些敏感私密的信息,更不能暴露出去.在这里给大家介绍一种在前端JS中的MD5加密算法(因为要匹配的后台数据是MD ...
- ACE在Ubuntu下的安装和编译
之前写了很多linux下的底层网络API的demo,这些demo可用于了解底层的网络通信过程,但是想做出好的服务器用于实际业务还是非常困难的,需要大量的代码实现,移植性也非常差,想要写出高性能架构的服 ...
- PATB1021个数统计
参考代码: #include<cstdio> #include<cstring> #include<cstdlib> int main() { char str[1 ...
- Centos开发小计
1. 生成静态库,linux下库的规则是lib开头 g++ -c code.cpp ar cr libcode.a code.o
- Beta/Gamma事后分析
目录 设想和目标 计划 资源 变更管理 设计/实现 测试/发布 团队的角色,管理,合作 总结 对照敏捷开发的原则, 你觉得你们小组做得最好的是哪几个原则? 请列出具体的事例. 照片 设想和目标 我们的 ...
- 【视频开发】伽马校正(gamma correction)学习笔记
我相信几乎所有做图像处理方面的人都听过伽马校正(Gamma Correction)这一个名词,但真正明白它是什么.为什么要有它.以及怎么用它的人其实不多.我也不例外. 最初我查过一些资料,但很多文章 ...
- A Philosophy of Software Design
关于复杂性,尚无统一的定义,从不同的角度可以给出不同的答案.可以用数量来度量,比如芯片集成的电子器件越多越复杂(不一定对):按层次性[2]度量,复杂度在于层次的递归性和不可分解性.在信息论中,使用熵来 ...
- 管道通信——FIFO的代码实现
一.用到的函数 umask linux中的 umask 函数主要用于:在创建新文件或目录时 屏蔽掉新文件或目录不应有的访问允许权限. 文件的访问允许权限共有9种,分别是 ...