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 ...
随机推荐
- 配置Maven环境变量-Eclipse/Idea添加Maven
1. 文件下载 官网下载地址:http://maven.apache.org/download.cgi 下方有我提供的下载链接. 由于下载缓慢,提供一份我的下载链接:https://www.lanzo ...
- Windbg的快捷键
窗口切换 可以使用以下键盘快捷方式窗口之间进行切换. 项 效果 CTRL+TAB 调试信息窗口之间切换. 通过重复使用此密钥,你可以扫描通过的所有窗口,而不考虑是否浮动. 停靠本身,或选项卡式停靠窗口 ...
- 【LG3647】[APIO2014]连珠线
[LG3647][APIO2014]连珠线 题面 洛谷 题解 首先考虑一下蓝线连起来的情况,一定是儿子-父亲-另一个儿子或者是儿子-父亲-父亲的父亲. 而因为一开始只有一个点在当前局面上,将一条红边变 ...
- 第02组 Alpha冲刺(2/4)
队名:十一个憨批 组长博客 作业博客 组长黄智 过去两天完成的任务:写博客,复习C语言 GitHub签入记录 接下来的计划:构思游戏实现 还剩下哪些任务:敲代码 燃尽图 遇到的困难:Alpha冲刺时间 ...
- vue bootstrap中modal对话框不显示遮挡打不开
使用Vue bootstrap时,点击modal却不能弹出来,被隐藏遮挡无法显示,参考下面的这个博客的说明解决了这个问题: Heap Stack Blog(pingbook.top)Vue boots ...
- Hotspot的栈
各种类型的线程他们所需要的栈的大小其实是可以通过不同的参数来控制的: java_thread的stack_size,其实就是-Xss或者-XX:ThreadStackSize的值 compiler_t ...
- 用ADB 抓取和存储APP日志方法
前置条件:电脑已经安装好adb (一) 进入adb目录下连接手机,检测出手机 CD 到SDK的platform-tools (二)adb logcat-c 清除日志 (三)adb logcat ...
- VisualSVN 新版本终于支持一个解决方案下多workcopy了,并解决了上个版本一个重要BUG
Multiple working copies within a single solution VisualSVN 7.0 and older require the solution file a ...
- TYPORA语法
原文链接:https://blog.csdn.net/SIMBA1949/article/details/79001226
- 在Windows 10中禁用自动文件夹类型发现
点击下载注册表文件:https://files.cnblogs.com/files/Music/win10_automatic_folder_type_discovery.zip 已知Windows ...