44import android .util .Base64 ;
55import android .util .Log ;
66import org .apache .http .*;
7+ import org .apache .http .client .HttpResponseException ;
78import org .apache .http .message .BasicLineParser ;
89import org .apache .http .message .BasicNameValuePair ;
910
1011import javax .net .SocketFactory ;
12+ import javax .net .ssl .SSLContext ;
13+ import javax .net .ssl .SSLException ;
1114import javax .net .ssl .SSLSocketFactory ;
15+ import javax .net .ssl .TrustManager ;
1216import java .io .EOFException ;
1317import java .io .IOException ;
1418import java .io .OutputStream ;
1519import java .io .PrintWriter ;
1620import java .net .Socket ;
1721import java .net .URI ;
22+ import java .security .KeyManagementException ;
23+ import java .security .NoSuchAlgorithmException ;
1824import java .util .List ;
1925
2026public class WebSocketClient {
@@ -29,6 +35,12 @@ public class WebSocketClient {
2935
3036 private final Object mSendLock = new Object ();
3137
38+ private static TrustManager [] sTrustManagers ;
39+
40+ public static void setTrustManagers (TrustManager [] tm ) {
41+ sTrustManagers = tm ;
42+ }
43+
3244 public WebSocketClient (URI uri , Handler handler , List <BasicNameValuePair > extraHeaders ) {
3345 mURI = uri ;
3446 mHandler = handler ;
@@ -59,7 +71,7 @@ public void run() {
5971 String originScheme = mURI .getScheme ().equals ("wss" ) ? "https" : "http" ;
6072 URI origin = new URI (originScheme , mURI .getSchemeSpecificPart (), null );
6173
62- SocketFactory factory = SSLSocketFactory .getDefault ();
74+ SocketFactory factory = mURI . getScheme (). equals ( "wss" ) ? getSSLSocketFactory () : SocketFactory .getDefault ();
6375 mSocket = factory .createSocket (mURI .getHost (), port );
6476
6577 PrintWriter out = new PrintWriter (mSocket .getOutputStream ());
@@ -83,7 +95,7 @@ public void run() {
8395 // Read HTTP response status line.
8496 StatusLine statusLine = parseStatusLine (readLine (stream ));
8597 if (statusLine .getStatusCode () != HttpStatus .SC_SWITCHING_PROTOCOLS ) {
86- throw new ProtocolException ( "Bad HTTP response: " + statusLine );
98+ throw new HttpResponseException ( statusLine . getStatusCode (), statusLine . getReasonPhrase () );
8799 }
88100
89101 // Read HTTP response headers.
@@ -104,6 +116,11 @@ public void run() {
104116 Log .d (TAG , "WebSocket EOF!" , ex );
105117 mHandler .onDisconnect (0 , "EOF" );
106118
119+ } catch (SSLException ex ) {
120+ // Connection reset by peer
121+ Log .d (TAG , "Websocket SSL error!" , ex );
122+ mHandler .onDisconnect (0 , "SSL" );
123+
107124 } catch (Exception ex ) {
108125 mHandler .onError (ex );
109126 }
@@ -113,7 +130,10 @@ public void run() {
113130 }
114131
115132 public void disconnect () throws IOException {
116- mSocket .close ();
133+ if (mSocket != null ) {
134+ mSocket .close ();
135+ mSocket = null ;
136+ }
117137 }
118138
119139 public void send (String data ) {
@@ -179,4 +199,10 @@ public interface Handler {
179199 public void onDisconnect (int code , String reason );
180200 public void onError (Exception error );
181201 }
202+
203+ private SSLSocketFactory getSSLSocketFactory () throws NoSuchAlgorithmException , KeyManagementException {
204+ SSLContext context = SSLContext .getInstance ("TLS" );
205+ context .init (null , sTrustManagers , null );
206+ return context .getSocketFactory ();
207+ }
182208}
0 commit comments