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 ...
随机推荐
- 1. vue 的安装
兼容性 Vue 不支持 IE8 及以下版本,因为 Vue 使用了 IE8 无法模拟的 ECMAScript 5 特性.但它支持所有兼容 ECMAScript 5 的浏览器. 安装: 1.直接用 < ...
- Scrapy笔记08- 文件与图片
Scrapy笔记08- 文件与图片 Scrapy为我们提供了可重用的item pipelines为某个特定的Item去下载文件. 通常来说你会选择使用Files Pipeline或Images Pip ...
- C++各大有名库的介绍——网络通信
ACE是C++库的代表,超重量级的网络通信开发框架.ACE自适配通信环境(Adaptive Communication Environment)是可以自由使用.开放源代码的面向对象框架,在其中实现了许 ...
- A 题解————2019.10.16
[题目描述] 对于给定的一个正整数n, 判断n是否能分成若干个正整数之和 (可以重复) ,其中每个正整数都能表示成两个质数乘积. [输入描述]第一行一个正整数 q,表示询问组数.接下来 q 行,每行一 ...
- 【border相关】【P3426】 [POI2005]SZA-Template
[border相关][P3426] [POI2005]SZA-Template Description 给定一个字符串 \(S\),要求一个最短的字符串 \(T\),使得 \(S\) 可以由 \(T\ ...
- 【线段树】【P4062】 [Code+#1]Yazid 的新生舞会
Description 给定一个长度为 \(n\) 的序列,求有多少子区间满足区间众数严格大于区间长度的一半.如果区间有多个出现次数最多且不同的数则取较小的数为众数. Limitation 对于全部的 ...
- 区间dp提升复习
区间\(dp\)提升复习 不得不说这波题真的不简单... 技巧总结: 1.有时候转移可以利用背包累和 2.如果遇到类似区间添加限制的题可以直接把限制扔在区间上,每次只考虑\([l,r]\)被\([i, ...
- Powershell更新
问题:在vin7电脑启动vagrant up 提示powershell版本过低. 在vin7电脑启动vagrant up 提示powershell版本过低: The version of powers ...
- 石锤了!google彻底断供华为,只能加速鸿蒙生态的形成
前言 操作系统是当今科技行业的灵魂,而即将推出这款操作系统是一个集电脑.手机.汽车等设备于一体的系统.如今手机行业里已经是一片红海了,竞争相当激烈,但是竞争归竞争,但是一旦扯上别的事就更麻烦了,像华为 ...
- IntelliJ IDEA 2019.2已经可以利用补丁永久破解激活了(持续更新)
前面的文章中,一直在强调2019系列的idea无法使用补丁进行永久激活,但是最近发现,已经有大佬可以利用补丁将idea 2019.2及以下版本激活到2089年了,而且还不用改hosts,实在是佩服,不 ...