Https接口调用工具类
ClientUtil.java
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class ClientUtil {
private String charset = "utf-8";
public String doPost(String url, Map<String,String> map){
HttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try{
httpClient = new SSLClient();
httpPost = new HttpPost(url);
//设置参数
List<NameValuePair> list = new ArrayList<NameValuePair>();
Iterator iterator = map.entrySet().iterator();
while(iterator.hasNext()){
Entry<String,String> elem = (Entry<String, String>) iterator.next();
list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
}
if(list.size() > 0){
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset);
httpPost.setEntity(entity);
}
HttpResponse response = httpClient.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,charset);
}
}
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
}
SSLClient.java
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
//用于进行Https请求的HttpClient
public class SSLClient extends DefaultHttpClient{
public SSLClient() throws Exception{
super();
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[]{tm}, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = this.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", 443, ssf));
}
}
Https接口调用工具类的更多相关文章
- Java接口调用工具类
package com.qiyuan.util; import java.io.BufferedReader; import java.io.IOException; import java.io.I ...
- Spring 远程调用工具类RestTemplateUtils
Spring 远程调用Rest服务工具类,包含Get.Post.Put.Delete四种调用方式. 依赖jar <dependency> <groupId>org.spring ...
- 调用http接口的工具类
网上面有很多,但是我们项目怎么也调不到结果,试了差不多很多案例,都是报connection reset 后来,我发现是有一个验证,需要跳过验证.然后才能调接口.所以找了一个忽略https的方法.进行改 ...
- java中模拟http(https)请求的工具类
在java中,特别是java web中,我们经常需要碰到的一个场景是我们需要从服务端去发送http请求,获取到数据,而不是直接从浏览器输入请求网址获得相应.比如我们想访问微信接口,获取其返回信息. 在 ...
- PJzhang:exiftool图片信息提取工具和短信接口调用工具TBomb
猫宁!!! 作者:Phil Harvey 这是图片信息提取工具的地址: https://sno.phy.queensu.ca/~phil/exiftool/ 网站隶属于Sudbury 中微子天文台,从 ...
- Java基础Map接口+Collections工具类
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
- spring -mvc service层调用工具类配置
在service层时调用工具类时服务返回工具类对象为空 在此工具类上加上@Component注解就可以了 @Component:把普通pojo实例化到spring容器中,相当于配置文件中的 <b ...
- java利用HttpClient进行https接口调用
1.为了避免需要证书,所以用一个类继承DefaultHttpClient类,忽略校验过程. import java.security.cert.CertificateException; import ...
- 发送http请求和https请求的工具类
package com.haiyisoft.cAssistant.utils; import java.io.IOException;import java.util.ArrayList; impor ...
随机推荐
- 第一章 大体知道java语法1----------能写java小算法
很多人开始学习java时,都是抱着诸如<Thinking in java>.<疯狂java>等书籍,从前到后慢慢翻看,不管其内容重要与否,也不关心自己以后能否使用到.我的建议是 ...
- idea如果发生@override is not allowed when implement 错误,可以在Project Structure-Modules中更改Language level,设置为6以上的。
- 获取客户端ip(包含中间反向代理)
/** * 根据request获取用户的IP地址 * @param request * @return */ public static String getRemoteHost(HttpServle ...
- idea出现灰色或者黄色的波浪线如何去除
1.File--setting--Editor-Inspections-Geneal-Duplicated Code 去除 主要是类中出现太多的重复代码,idea自动提示.
- python 构建自己的log系统
python的logging模块提供了记录程序运行情况的日志功能,类似于Apache的log4j,很好很强大,这里我们就来看一下Python中内置的日志模块logging用法详解 logging模块简 ...
- 简单说 JavaScript实现雪花飘落效果
说明 这次实现的雪花飘落的效果很简单,主要是为了练习练习JavaScript中的定时器,setTimeout 和 setInterval. 效果图 解释setTimeout() setTimeout函 ...
- Maven-SSM框架整合
1.创建Maven项目 配置pom.xml依赖 <!-- 允许创建jsp页面 --> <dependency> <groupId>javax.servlet< ...
- Redis 持久化配置(兼论瞎翻译的问题)
redis.conf 文件中配置 save 默认配置项: save save save 查了好几个地方都是这么说的: 第一句的意思:15分钟内修改了一个键就保存??? 往 Redis 里加了一个值,重 ...
- iOS 企业版 打包
iOS 企业版 打包 使用 iOS 企业版的证书发布应用可以跳过 Appstore,直接发布到自己的网站,让其他人在自己的网站上下载安装,但前提是要用苹果自带的Safari 浏览器才能下载安装,其他浏 ...
- ubuntu源与常用python配置pip源(win)、pip常用命令
pip常用命令 ubuntu更新系统源 首先备份/etc/apt/sources.list mv /etc/apt/sources.list /etc/apt/sources.list.bak 然后下 ...