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 ...
随机推荐
- LeetCode 622. Design Circular Queue
原题链接在这里:https://leetcode.com/problems/design-circular-queue/ 题目: Design your implementation of the c ...
- Windows server 2003 粘滞键后门+提权
Windows server 2003中可以建立粘滞键与cmd的连接来绕过已经设置好的安全机制做一些事情,比如新建用户.提权 粘滞键介绍 网上查了一些资料,也没怎么说明白,不如自己试一下,大概意思就是 ...
- java 补充(final、static)
final 固定的 final 修饰类的时候,只能作为子类继承,不能作为父类. final 定义变量时,必须给成员变量赋值.------ 1.直接赋值 2.构造方法. final 修饰成员方法时 ...
- CF264D - Colorful Stones 题解
题面 官方题解 模拟赛题解 题解概述: 定义符号A~B表示序列A是序列B的子序列,A!~B反之. 设操作序列为I,则有A~I,B!~I,C~I,D!~I. 可得出条件①B!~C且D!~A,所以我们只要 ...
- ZROI 暑期高端峰会 A班 Day6 DP
[THUPC2018]城市地铁规划 (日常讲题之前 YY--) 一眼出 \(O(n^3+nk)\) 做法. \(dp[i][j]\) 表示前 \(i\) 个点,前 \(i\) 个点度数和为 \(j\) ...
- Computer-Hunters——团队展示
Computer-Hunters--团队展示 这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzu/2019FZUSEZ 这个作业要求在哪里 https://ed ...
- nginx配置神器
原文 https://mp.weixin.qq.com/s/zFEk7XzHj3xPReDXEnQxcQ https://nginxconfig.io/ Nginx作为一个轻量级的HTTP服务器,相比 ...
- BuaaRedSun团队博客目录——北航社团项目
目录 一.Scrum Meeting 1. Alpha 2. Beta 3. Gamma 二.测试报告 三.发布说明 四.技术博客 后端 前端 五.习得的软工原理/方法/技能? Alpha Beta ...
- Android集成C程序访问驱动设备节点
1.前言 Android系统中,应用程序一般是使用Java语言进行开发的,但是通过C语言也可以进行Android中的可执行程序开发,接下来,将简单介绍在Android系统中如何通过C程序来访问内核中s ...
- 把jar包安装到本地Maven仓库
使用的场景 自己写的工具类想安装到本地 从Maven仓库中下载不下来的jar 使用的步骤 首先要保证自己的Maven配置全局环境变量,如果没有配置过maven全局变量,可以按照下面的步骤配 ...