- 浏览: 269677 次
- 性别:
- 来自: 天津
文章分类
- 全部博客 (183)
- oracle (4)
- informix (1)
- web开发 (6)
- java (49)
- hibernate (1)
- hadoop (1)
- spring (23)
- 非技术 (8)
- ibatis2 (5)
- Linux (6)
- tomcat (14)
- nginx (7)
- dubbo (3)
- myibatis (7)
- webservice 开发 (2)
- mysql (2)
- svn (2)
- redis (7)
- 分布式技术 (17)
- zookeeper (2)
- kafka (2)
- velocity (1)
- maven (7)
- js (1)
- freemarker (1)
- Thymeleaf (3)
- 代码审计 (1)
- ibatis3 (1)
- rabbitmq (1)
最新评论
package com.gpcsoft.xjmodule.utils;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.net.ssl.SSLContext;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HttpsClient {
private static final Logger log = LoggerFactory.getLogger(HttpsClient.class);
public static final String get(final String url, final Map<String, Object> params) {
StringBuilder sb = new StringBuilder("");
if (null != params && !params.isEmpty()) {
int i = 0;
for (String key : params.keySet()) {
if (i == 0) {
sb.append("?");
} else {
sb.append("&");
}
sb.append(key).append("=").append(params.get(key));
i++;
}
}
CloseableHttpClient httpClient = createSSLClientDefault();
CloseableHttpResponse response = null;
HttpGet get = new HttpGet(url + sb.toString());
String result = "";
try {
response = httpClient.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
if (null != entity) {
result = EntityUtils.toString(entity, "UTF-8");
}
}
} catch (IOException ex) {
log.error(ex.getMessage());
} finally {
if (null != response) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException ex) {
log.error(ex.getMessage());
}
}
}
return result;
}
public String postString(final String url, final Map<String, String> params) {
CloseableHttpClient httpClient = createSSLClientDefault();
HttpPost post = new HttpPost(url);
CloseableHttpResponse response = null;
if (null != params && !params.isEmpty()) {
List<NameValuePair> nvpList = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : params.entrySet()) {
NameValuePair nvp = new BasicNameValuePair(entry.getKey(), entry.getValue());
nvpList.add(nvp);
}
post.setEntity(new UrlEncodedFormEntity(nvpList, Charset.forName("UTF-8")));
}
String result = "";
try {
response = httpClient.execute(post);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
if (null != entity) {
result = EntityUtils.toString(entity, "UTF-8");
}
log.info("result:"+result);
}
} catch (IOException ex) {
ex.printStackTrace();
log.error(ex.getMessage());
} finally {
if (null != response) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException ex) {
log.error(ex.getMessage());
}
}
}
return result;
}
private static CloseableHttpClient createSSLClientDefault() {
SSLContext sslContext;
try {
sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
//信任所有
@Override
public boolean isTrusted(X509Certificate[] xcs, String string){
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
} catch (KeyStoreException ex) {
ex.printStackTrace();
log.error(ex.getMessage());
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
log.error(ex.getMessage());
} catch (KeyManagementException ex) {
ex.printStackTrace();
log.error(ex.getMessage());
}
return HttpClients.createDefault();
}
public static void config(){
// System.setProperty("https.protocols", "TLSv1.2,TLSv1.1,SSLv3,TLSv1.0,SSLv2");
String path = System.getProperty("user.dir")+File.separator
+"src/main/resources/sys"+File.separator+"log4j.properties";
System.out.println("path::"+path);
PropertyConfigurator.configure(path);
}
public static void zbxx() {
HttpsClient post = new HttpsClient();
try {
//String url = "http://192.168.2.215/msn/sendmsn.do";
// String url ="http://127.0.0.1:8000/m/msn/sendmsn.do";
//String url ="http://ztest.esgcc.com.cn:64515/m/msn/sendmsn.do";
String url ="https://dl.guowang.com:8443/springmvc/msn/sendmsn.do";
Map<String, String> param = new HashMap<String, String>();
param.put("proId", "4028824263e2e26c0163e2e61c7b0008");
param.put("bag", "");
param.put("xjUrl",
"https://z.esgcc.com.cn/XjPortalHome.do?method=indexAction&rp=10&page=1");
String name = URLEncoder.encode("新疆发送短信13", "UTF-8");
param.put("name", name);
param.put("code", "zbxx");
String result = post.postString(url, param);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
config();
zbxx();
}
}
注意:一定要使用httpclient-4.3.5.jar,httpcore-4.3.2.jar。如果使用的话,每次调用的时候提示握手失败;
可以使用:httpclient-4.3.6.jar,httpcore-4.3.6.jar 本实例就是使用了这个版本。
Tomcat 版本配置SSL过程有两步
1用JDK自带的keytool.exe来生成私有密钥和自签发的证书,如下:
keytool -genkey -keyalg RSA -alias tomcat
2修改Tomcat的conf\server.xml文件,即增加下面一段
tomcat6 在server.xml
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystroke="${user.home}/.keystore" keystorePass="changeit" />
tomcat7
<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
keystoreFile="${user.home}/.keystore" keystorePass="changeit"
clientAuth="false" sslProtocol="TLS" />
keystoreFile:证书存放位置
keystorePass:证书密码
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.net.ssl.SSLContext;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HttpsClient {
private static final Logger log = LoggerFactory.getLogger(HttpsClient.class);
public static final String get(final String url, final Map<String, Object> params) {
StringBuilder sb = new StringBuilder("");
if (null != params && !params.isEmpty()) {
int i = 0;
for (String key : params.keySet()) {
if (i == 0) {
sb.append("?");
} else {
sb.append("&");
}
sb.append(key).append("=").append(params.get(key));
i++;
}
}
CloseableHttpClient httpClient = createSSLClientDefault();
CloseableHttpResponse response = null;
HttpGet get = new HttpGet(url + sb.toString());
String result = "";
try {
response = httpClient.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
if (null != entity) {
result = EntityUtils.toString(entity, "UTF-8");
}
}
} catch (IOException ex) {
log.error(ex.getMessage());
} finally {
if (null != response) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException ex) {
log.error(ex.getMessage());
}
}
}
return result;
}
public String postString(final String url, final Map<String, String> params) {
CloseableHttpClient httpClient = createSSLClientDefault();
HttpPost post = new HttpPost(url);
CloseableHttpResponse response = null;
if (null != params && !params.isEmpty()) {
List<NameValuePair> nvpList = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : params.entrySet()) {
NameValuePair nvp = new BasicNameValuePair(entry.getKey(), entry.getValue());
nvpList.add(nvp);
}
post.setEntity(new UrlEncodedFormEntity(nvpList, Charset.forName("UTF-8")));
}
String result = "";
try {
response = httpClient.execute(post);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
if (null != entity) {
result = EntityUtils.toString(entity, "UTF-8");
}
log.info("result:"+result);
}
} catch (IOException ex) {
ex.printStackTrace();
log.error(ex.getMessage());
} finally {
if (null != response) {
try {
EntityUtils.consume(response.getEntity());
} catch (IOException ex) {
log.error(ex.getMessage());
}
}
}
return result;
}
private static CloseableHttpClient createSSLClientDefault() {
SSLContext sslContext;
try {
sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
//信任所有
@Override
public boolean isTrusted(X509Certificate[] xcs, String string){
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
} catch (KeyStoreException ex) {
ex.printStackTrace();
log.error(ex.getMessage());
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
log.error(ex.getMessage());
} catch (KeyManagementException ex) {
ex.printStackTrace();
log.error(ex.getMessage());
}
return HttpClients.createDefault();
}
public static void config(){
// System.setProperty("https.protocols", "TLSv1.2,TLSv1.1,SSLv3,TLSv1.0,SSLv2");
String path = System.getProperty("user.dir")+File.separator
+"src/main/resources/sys"+File.separator+"log4j.properties";
System.out.println("path::"+path);
PropertyConfigurator.configure(path);
}
public static void zbxx() {
HttpsClient post = new HttpsClient();
try {
//String url = "http://192.168.2.215/msn/sendmsn.do";
// String url ="http://127.0.0.1:8000/m/msn/sendmsn.do";
//String url ="http://ztest.esgcc.com.cn:64515/m/msn/sendmsn.do";
String url ="https://dl.guowang.com:8443/springmvc/msn/sendmsn.do";
Map<String, String> param = new HashMap<String, String>();
param.put("proId", "4028824263e2e26c0163e2e61c7b0008");
param.put("bag", "");
param.put("xjUrl",
"https://z.esgcc.com.cn/XjPortalHome.do?method=indexAction&rp=10&page=1");
String name = URLEncoder.encode("新疆发送短信13", "UTF-8");
param.put("name", name);
param.put("code", "zbxx");
String result = post.postString(url, param);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
config();
zbxx();
}
}
注意:一定要使用httpclient-4.3.5.jar,httpcore-4.3.2.jar。如果使用的话,每次调用的时候提示握手失败;
可以使用:httpclient-4.3.6.jar,httpcore-4.3.6.jar 本实例就是使用了这个版本。
Tomcat 版本配置SSL过程有两步
1用JDK自带的keytool.exe来生成私有密钥和自签发的证书,如下:
keytool -genkey -keyalg RSA -alias tomcat
2修改Tomcat的conf\server.xml文件,即增加下面一段
tomcat6 在server.xml
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystroke="${user.home}/.keystore" keystorePass="changeit" />
tomcat7
<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
keystoreFile="${user.home}/.keystore" keystorePass="changeit"
clientAuth="false" sslProtocol="TLS" />
keystoreFile:证书存放位置
keystorePass:证书密码
发表评论
-
aop实现通用缓存,并且防止缓存击穿
2019-09-16 15:10 795实现代码在附件中 1.自定义注解文件 package sgn ... -
统计多线程下程序运行总时间
2019-05-15 16:55 1097package com.gpcsoft.hct.epp.egp ... -
通过模板的方式解决缓存被击穿的问题
2019-04-15 11:35 4081. package gjp.tools; import c ... -
Rsa 加解密算法
2019-03-18 10:27 366package gjp.tools; /** * @Aut ... -
httpClient 使用http协议上传文件
2018-10-09 15:58 3102<dependency> <grou ... -
httpClient 使用HTTPS 协议上传文件
2018-09-30 14:50 2435<dependency> <group ... -
防止 XML外部实体注入
2018-09-18 17:03 7306方式一 DocumentBuilderFactory dbf ... -
猎狗方式调用接口
2017-09-27 08:36 645package boce.hit.dog; import j ... -
netty 实现长连接
2017-08-24 09:52 13091.server 端信息 package com.boce.n ... -
netty 开发入门
2017-08-22 14:30 6671.准备jar <properties> & ... -
nio 编程实例
2017-08-16 14:15 5971.编写服务端 package com.boce.nio.s ... -
jwt 生成token 和解析token
2017-06-06 16:45 5914<jjwt.version>0.6.0</j ... -
实现Java高并发隔离 模拟
2017-05-08 10:34 513package org; import java.util. ... -
java 命令
2017-04-20 16:42 416java 命令: java -Djava.ext.dirs ... -
nio 通讯
2017-04-01 15:41 525nio 服务端: package nio.study.se ... -
HashMap 便利时不按照输入顺序输出
2017-03-27 17:11 1954使用:hashmap传输数据时,便利map中的数据时,发现 ... -
使用Lock,对不同商品加锁
2017-03-13 10:52 1247package com.boce.gbkutf; ... -
json 转泛型的集合类
2017-03-07 16:21 1212package com.boce.test; ... -
httpclient4.5 使用post方式提交请求
2017-03-03 11:00 2014private RequestConfig req ... -
GBK与UTF-8 字符串互转
2017-02-24 11:17 2206package com.cloud.tools; i ...
相关推荐
当涉及到HTTPS接口调用时,HttpClient可以处理加密通信,确保数据传输的安全性。在这个主题中,我们将深入探讨如何在Java中使用HttpClient来实现HTTPS接口调用。 首先,我们需要理解HTTPS协议。HTTPS是HTTP(超文本...
在本文中,我们将深入探讨如何使用HttpClient调用WebService。 首先,调用WebService通常涉及SOAP(Simple Object Access Protocol)或RESTful API。HttpClient可以处理这两种类型的Web服务。在本示例中,我们假设...
### WebApi系列-通过HttpClient来调用WebApi接口 #### 一、概述 在现代Web开发中,Web API已经成为一种非常流行的模式,用于提供服务端与客户端之间的数据交互。随着.NET Framework以及.NET Core的发展,微软提供...
### 使用HttpClient调用WebService接口详解 #### 一、概述 在现代软件开发中,Web服务(尤其是WebService)作为实现系统间交互的一种重要手段被广泛采用。WebService提供了通过HTTP协议进行远程过程调用的能力,...
本demo有两个例子test是作为服务端开启的,httpclient是客户端调用的,模拟服务端的test在启动后是没有界面的,要加上id才能看到json数据,httpclient的HttpClient33.java是测试类,最后的结果是接受到一个对象。...
使用httpclient调用公安部接口 并接受返回的字符串转换为图片
在IT行业中,HttpClient是一个常用的Java库,用于执行HTTP和HTTPS请求。这个实例主要涉及如何配置HttpClient来忽略SSL(Secure Socket Layer)验证,这对于在开发和测试环境中处理自签名证书或未认证的服务器非常...
这里我们探讨的是如何使用HttpClient调用WCF服务。 HttpClient是Apache的一个开源项目,提供了强大的HTTP协议处理能力,可以用于发送HTTP请求并接收响应。在Java中,HttpClient库是一个常用的工具,它使得开发者...
HttpClient还可以处理HTTPS连接,支持SSL/TLS协议,确保数据传输的安全性。在配置HttpClient时,可能需要设置信任的证书和密钥库。 10. **错误处理**: 工具类可能会包含对异常的捕获和处理,如网络连接失败、...
在WPF(Windows Presentation Foundation)应用程序中,使用HttpClient调用Web API是一项常见的任务,尤其是在进行数据交互时。本文将深入探讨如何在WPF应用中利用HttpClient进行异步操作,以避免阻塞主线程,提高...
总之,通过HttpClient调用Web服务是一个涉及网络通信和HTTP协议理解的过程。在实际开发中,了解HttpClient的使用方法和最佳实践,可以帮助我们构建稳定、高效的Web服务客户端。结合Maven构建工具,可以轻松管理项目...
它是由Apache软件基金会开发的,被广泛应用于各种Web服务的客户端实现。本篇文章将深入探讨如何使用HttpClient方式调用URL,...在项目"thirdTest"中,你可以尝试应用这些概念和代码示例来实现HttpClient调用URL的功能。
在这个例程中,我们将深入探讨如何利用HttpClient来调用一个天气预报接口,并解析返回的JSON数据。以下是一些关键知识点: 1. **HttpClient库**:HttpClient是Apache提供的一个开源库,它允许开发者构建HTTP客户端...
以下是一个关于如何使用HttpClient调用JavaEye API的基本步骤: 1. **导入必要的库**:首先,你需要在项目中导入HttpClient相关的依赖库,通常包括httpclient、httpcore和httpmime等。如果你使用的是Maven或Gradle...
"JAVA利用HttpClient进行HTTPS接口调用的方法" 知识点1:HttpClient的基本概念 HttpClient是一个开放源代码的Java库,用于提供高效、灵活和可扩展的HTTP客户端解决方案。HttpClient支持HTTP/1.1和HTTP/2.0协议,...
httpclient调用webservice.txt
本篇将详细讲解使用HttpClient调用WebService所需的jar包以及它们的功能。 首先,我们来看标题提到的"Httpclient调用WebService所需jar包"。HttpClient是一个灵活且功能丰富的Java库,它允许开发人员执行各种HTTP...
httpClient远程调用工具类...
HttpClient之Https应用实例~ 包含: HttpClient 使用代理访问Https HttpClient 信任所有主机-对于任何证书都不做检查 HttpClient 允许所有证书的校验(包括过期证书)
HttpClient接口调用工具类 (Post请求 get请求 put请求 delete请求 (附带调用demo) 下载就可以用 ) 可设置httpclient的连接池大小,连接池最大并发连接数,单路由最大并发数设