首页 > 学院 > 开发设计 > 正文

WebSocket连接wss链接

2019-11-09 17:49:46
字体:
来源:转载
供稿:网友

参考链接:http://www.juliankrone.com/connect-and-transfer-data-with-secure-websockets-in-android/

WebSocket正常连接http开头的链接都没什么问题,但是连接wss开头的链接IOS没有问题,安卓这边就连接不上了。通过查找资料发现需要对WebSocketClient设置setWebSocketFactory()这个方法。在new完WebSocketClient后面加入以下代码再进行连接就可以了。

// WebSocket连接wss链接				// This part is needed in case you are going to use self-signed				// certificates				TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {					public java.security.cert.X509Certificate[] getAcceptedIssuers() {						return new java.security.cert.X509Certificate[] {};					}					public void checkClientTrusted(X509Certificate[] chain,							String authType) throws CertificateException {					}					public void checkServerTrusted(X509Certificate[] chain,							String authType) throws CertificateException {					}				} };				SSLContext sc = SSLContext.getInstance("TLS");				sc.init(null, trustAllCerts, new java.security.SecureRandom());				// Otherwise the line below is all that is needed.				// sc.init(null, null, null);				wsc.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(sc));

这里需要用WebSocket的1.3.0的jar包,下载链接:http://grepcode.com/snapshot/repo1.maven.org/maven2/org.java-websocket/Java-WebSocket/1.3.0/

AS的话直接:

compile'org.java-websocket:Java-WebSocket:1.3.0'

权当自己做个记录啦~


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表