android https 连接建立
来源:互联网 发布:ios视频录制软件 编辑:程序博客网 时间:2024/06/06 02:26
http://blog.csdn.net/totogo2010/article/details/6368069
Https与Http类似,只不过Https一般是通过post请求服务器,但是Https与http不同的是Https与服务器会话是处于连接状态。http则发送请求后连接就会断开。
发送post请求代码:
String query = r4 + "&pass=" + r3; //请求参数
byte[] entitydata = query.getBytes();//得到实体数据
HttpsURLConnection urlCon = (new URL(ticketurl)).openConnection();
urlCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
urlCon.setRequestProperty("Content-Length", String.valueOf(entitydata.length));
((HttpsURLConnection) urlCon).setRequestMethod("POST");
urlCon.setDoOutput(true);
urlCon.setDoInput(true);
urlCon.connect();
//把封装好的实体数据发送到输出流
OutputStream outStream = urlCon.getOutputStream();
outStream.write(entitydata);
outStream.flush();
outStream.close();
//服务器返回输入流并读写
BufferedReader in = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
return line;
}
in.close();
另外使用HttpsURLConnection时需要实现HostnameVerifier 和 X509TrustManager,这两个实现是必须的,要不会报安全验证异常。然后初始化X509TrustManager中的SSLContext,为javax.net.ssl.HttpsURLConnection设置默认的SocketFactory和HostnameVerifier。代码如下:
private myX509TrustManager xtm = new myX509TrustManager();
private myHostnameVerifier hnv = new myHostnameVerifier();
public HttpsURLConnectionTest() {
//初始化X509TrustManager中的SSLContext
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("TLS");
X509TrustManager[] xtmArray = new X509TrustManager[] { xtm };
sslContext.init(null, xtmArray, new java.security.SecureRandom());
} catch (GeneralSecurityException gse) {
}
//为javax.net.ssl.HttpsURLConnection设置默认的SocketFactory和HostnameVerifier
if (sslContext != null) {
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
}
HttpsURLConnection.setDefaultHostnameVerifier(hnv);
这样就不会报错了。
- android https 连接建立
- android https 连接建立
- android https 连接建立
- android https 连接建立
- Https建立连接过程
- https 建立连接过程
- https建立连接
- https 建立连接过程
- HTTPS建立连接的过程
- android https安全连接
- HTTPS建立连接都发生了什么
- 深入揭秘HTTPS安全问题&连接建立全过程
- HTTPS 建立连接的详细过程
- HTTPS 建立连接的详细过程
- HTTPS研究(2)—分解HTTPS连接建立过程
- HTTPS为什么安全 &分析 HTTPS 连接建立全过程
- HTTPS为什么安全 &分析 HTTPS 连接建立全过程
- HTTPS为什么安全 &分析 HTTPS 连接建立全过程
- 一个30年老程序员的回顾(八)
- 浅谈Web系统的三种模型
- 模式设计(三)------状态模式
- 在MFC的单文档里,每个类获得其他类指针的一套方法
- ASM 之 Hello World(附)
- android https 连接建立
- 数据探查(一)
- 一种缓存切片更新问题的解决方法
- informatica转换
- Can't find model for source store
- The model used to open the store is incompatible with the one used to create the store
- AVLTree 二叉平衡树 实现
- 2012.11
- List of some journal impact factors