HttpClient 实现https访问
来源:互联网 发布:linux卸载phpmyadmin 编辑:程序博客网 时间:2024/05/16 19:36
一,java使用 keytool工具创建证书
CMD窗口中运行 "keytool -genkey -alias "别名" -keyalg RSA -validity "有效时间" -keystore "生成证书路径"
二,配制tomcat支持ssl
在tomcat的config server.xml中打开 Connector结点
<Connector port="443" protocol="org.apache.coyote.http11.Http11Protocol" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
keystoreFile="yourPath/xx.keystore" keystorePass="yourPwd"
clientAuth="false" sslProtocol="TLS" />
这里注意下,在tomcat6下
protocol="HTTP/1.1 "
<!-- 改为 -->
protocol="org.apache.coyote.http11.Http11Protocol"
否则起动会报错。
三,工程中web.xml配制
上两部己实现了ssl的访问 https://localst (443为https默认端口所以不用加端口号)就己是ssl安全通道中,但是我们还是可以通过http://localhost:port 访问,这里要让它http时自动成https
<security-constraint>
<web-resource-collection>
<web-resource-name>SSL</web-resource-name>
<url-pattern>/xxx/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
在url-pattern中配制的是你要拦截的请求路径,比如只想对login模块强制转成https,则可配 /login/*
四,此时让tomcat支持https己全部配制完成,浏览器第一次访问时因为是自己制作的证书可以会有安全信任问题,要添加例外。想要httpclient发送https的请求,则还需如下的步骤
五,从浏览器中导出让才的证书(crt,pem后缀都可以)然后用keytool工具导入keystore,命令如下:
keytool -import -alias "localhost" -file xxx.crt -keystore ooo.keystore
xxx.crt : 刚从浏览器导出的证书
ooo.keystore :命令生成的keystore文件
六,java代码 (不得不说httpclient3和4版本在使用上差异还很大,用习惯了3总感觉4并不太顺手)
public static String sendSSLRequest(String requestUrl) {
long responseLength = 0; // 响应长度
String responseContent = null; // 响应内容
HttpClient httpClient = new DefaultHttpClient(); // 创建默认的httpClient实例
KeyStore trustStore = null;
InputStream fis = null;
HttpGet httpGet = null;
HttpResponse response = null;
try {
trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
fis = HttpsBaseAction.class.getClassLoader().getResourceAsStream(
"my.keystore");
trustStore.load(fis, "pwd".toCharArray()); // 加载KeyStore
SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore); // 创建Socket工厂,将trustStore注入
Scheme sch = new Scheme("https", 443, socketFactory); // 创建Scheme
httpClient.getConnectionManager().getSchemeRegistry().register(sch); // 注册Scheme
httpGet = new HttpGet(requestUrl); // 创建HttpGet
Header headers[] = { new BasicHeader("myheader", "value") };
httpGet.setHeaders(headers); // 设置头信息
// 设置请求参数,get好像只能在url那里用?xx=xx传参(暂时没找到到别的可传参方法)
// 如用post传参如下
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("prameName", "value"));
UrlEncodedFormEntity uefEntity;
uefEntity = new UrlEncodedFormEntity(formparams, HTTP.UTF_8);
HttpPost post = new HttpPost(requestUrl);
post.setEntity(uefEntity);
post.setHeader("myheader", "value");
// response = httpClient.execute(post); // 执行POST请求
response = httpClient.execute(httpGet); // 执行GET请求
HttpEntity entity = response.getEntity(); // 获取响应实体
if (null != entity) {
responseLength = entity.getContentLength();
responseContent = EntityUtils.toString(entity, "UTF-8");
EntityUtils.consume(entity); // Consume response content
}
System.out.println("请求地址: " + httpGet.getURI());
System.out.println("响应状态: " + response.getStatusLine());
System.out.println("响应长度: " + responseLength);
System.out.println("响应内容: " + responseContent);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fis.close();
} finally {
httpClient.getConnectionManager().shutdown(); // 关闭连接,释放资源
return responseContent;
}
}
}
- HttpClient 实现https访问
- 通过HttpClient实现访问HTTPS网站
- android HttpClient 访问https
- httpclient 访问https
- httpclient 访问https应用
- httpclient访问https
- httpclient-访问https
- 如何使用HttpsURLConnect和HttpClient实现访问https
- httpclient 实现https请求
- httpclient来进行https访问
- Android 实现 HttpClient 请求Https
- 使用httpclient来进行https访问
- [Android实例] Android 使用HttpClient访问https
- java httpclient 无证书访问 https
- HttpClient 使用代理访问Https【转】
- HttpClient忽略证书访问HTTPS接口
- android httpClient 支持HTTPS的访问方式
- HttpClient忽略证书访问HTTPS接口
- js写的中国象棋程序
- MySQL时间戳转换
- QT 4.7支持中文(QT4.7)(中文)(makeqpf)
- Action名称的搜索顺序及配置的各项默认值
- 巧妙处理StringBuilder.AppendFormat 方法中字符串含有花括号“{}”
- HttpClient 实现https访问
- 关于LoadRunner录制脚本时,报REASON: Unable to connect to remote server: rc = -1 , le = 0错误
- UML笔记
- Java时间日期格式转换
- mysql 左右连接 以及内连接 多表连接
- VRML---第二章第二部分(造型外观属性)
- 使用Docx4j创建word文档
- iOS 遍历Document 文件夹 并获取文件属性
- jquery获取父窗口的元素