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 ...
随机推荐
- 10 使用 OpenCV、Kafka 和 Spark 技术进行视频流分析
问题引起 基于分布式计算框架Spark的室内防盗预警系统 首先用摄像头录一段视频,存在电脑里,下载一个ffmpeg的软件对视频进行处理,处理成一张张图片,然后通过hadoop里边的一个文件系统叫做hd ...
- 从零和使用mxnet实现softmax分类
1.softmax从零实现 from mxnet.gluon import data as gdata from sklearn import datasets from mxnet import n ...
- 【luoguP2252】 取石子游戏
题目链接 定义\(f[i][j]\)表示\(a=i,b=j\)时是必胜态还是必败态,博弈DP可以解决\(a,b \leq 100\) 的情况 然后就可以找规律了,发现\(f[i][j]=0\)的情况很 ...
- 什么是SQL Server2019大数据群集?
从SQL Server 2019(15.x)开始,SQL Server大数据群集允许您部署在Kubernetes上运行的SQL Server,Spark和HDFS容器的可伸缩群集.这些组件并排运行,使 ...
- Qt QThread两种方式的使用:1-继承QThread重写run函数; 2- 继承QObject并moveToThread && 消息和槽在线程和依附线程间的传递
2019年08月18日起笔 方式一:继承QThread重写run函数 MyThread.h ----------------------------------- ... class MyThread ...
- 对C++11中的`移动语义`与`右值引用`的介绍与讨论
本文主要介绍了C++11中的移动语义与右值引用, 并且对其中的一些坑做了深入的讨论. 在正式介绍这部分内容之前, 我们先介绍一下rule of three/five原则, 与copy-and-swap ...
- builder模式实例
package heapStark.blogCode.designPattern.builder; public class BaseBean { private int age; private S ...
- 作业:用pygame实现俄罗斯方块
用Pygame实现俄罗斯方块 参考资料后我安装了Pygame,参考了网上的代码实现了俄罗斯方块小游戏.我试着理解网上的代码的原理和含义,对这些代码的原理有了一个粗略地理解,代码通过参数,RGB值等来实 ...
- Bash cat EOF
cat <<EOF > ciphers.txt> ECDHE-ECDSA-AES128-GCM-SHA256> ECDHE-RSA-AES128-GCM-SHA256&g ...
- POJ-最大连续子序列和
给定一个整数序列,找到一个具有最大和的连续子序列(子序列最少包含一个元素),返回其最大和. 实例输入: -2, 1, -3, 4, -1, 2, 1, -5, 4 实例输出: 6(连续子序列4, -1 ...