org.apache.http.HttpStatus#SC_SWITCHING_PROTOCOLS源码实例Demo

下面列出了org.apache.http.HttpStatus#SC_SWITCHING_PROTOCOLS 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: Leanplum-Android-SDK   文件: WebSocketClient.java
public void connect() {
  if (mThread != null && mThread.isAlive()) {
    return;
  }

  mThread = new Thread(new Runnable() {
    @Override
    public void run() {
      try {
        int port = (mURI.getPort() != -1) ? mURI.getPort() : (isSecure() ? 443 : 80);

        String path = TextUtils.isEmpty(mURI.getPath()) ? "/" : mURI.getPath();
        if (!TextUtils.isEmpty(mURI.getQuery())) {
          path += "?" + mURI.getQuery();
        }

        URI origin = null;
        try {
          origin = new URI(mURI.getScheme(), "//" + mURI.getHost(), null);
        } catch (URISyntaxException e) {
          Util.handleException(e);
        }

        SocketFactory factory;
        try {
          factory = isSecure() ? getSSLSocketFactory() : SocketFactory.getDefault();
        } catch (GeneralSecurityException e) {
          Util.handleException(e);
          return;
        }
        try {
          mSocket = factory.createSocket(mURI.getHost(), port);
        } catch (IOException e) {
          Util.handleException(e);
        }

        PrintWriter out = new PrintWriter(mSocket.getOutputStream());
        out.print("GET " + path + " HTTP/1.1\r\n");
        out.print("Upgrade: websocket\r\n");
        out.print("Connection: Upgrade\r\n");
        out.print("Host: " + mURI.getHost() + "\r\n");
        out.print("Origin: " + (origin != null ? origin.toString() : "unknown") + "\r\n");
        out.print("Sec-WebSocket-Key: " + createSecret() + "\r\n");
        out.print("Sec-WebSocket-Version: 13\r\n");
        if (mExtraHeaders != null) {
          for (NameValuePair pair : mExtraHeaders) {
            out.print(String.format("%s: %s\r\n", pair.getName(), pair.getValue()));
          }
        }
        out.print("\r\n");
        out.flush();

        HybiParser.HappyDataInputStream stream = new HybiParser.HappyDataInputStream(mSocket.getInputStream());

        // Read HTTP response status line.

        StatusLine statusLine = parseStatusLine(readLine(stream));

        if (statusLine == null) {
          throw new HttpException("Received no reply from server.");
        } else if (statusLine.getStatusCode() != HttpStatus.SC_SWITCHING_PROTOCOLS) {
          throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
        }

        // Read HTTP response headers.
        String line;
        while (!TextUtils.isEmpty(line = readLine(stream))) {
          Header header = parseHeader(line);
          if (header.getName().equals("Sec-WebSocket-Accept")) {
            // FIXME: Verify the response...
          }
        }

        mListener.onConnect();

        // Now decode websocket frames.
        mParser.start(stream);

      } catch (EOFException ex) {
        Log.d("WebSocket EOF!", ex);
        mListener.onDisconnect(0, "EOF");

      } catch (SSLException ex) {
        // Connection reset by peer
        Log.d("Websocket SSL error!", ex);
        mListener.onDisconnect(0, "SSL");

      } catch (Exception e) {
        mListener.onError(e);
      }
    }
  });
  mThread.start();
}
 
源代码2 项目: aptoide-client   文件: WebSocketClient.java
public void connect() {
    if (mThread != null && mThread.isAlive()) {
        return;
    }

    mThread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                int port = (mURI.getPort() != -1) ? mURI.getPort() : ((mURI.getScheme().equals("wss") || mURI.getScheme().equals("https")) ? 443 : 80);

                String path = TextUtils.isEmpty(mURI.getPath()) ? "/" : mURI.getPath();
                if (!TextUtils.isEmpty(mURI.getQuery())) {
                    path += "?" + mURI.getQuery();
                }

                String originScheme = mURI.getScheme().equals("wss") ? "https" : "http";
                URI origin = new URI(originScheme, "//" + mURI.getHost(), null);

                SocketFactory factory = (mURI.getScheme().equals("wss") || mURI.getScheme().equals("https")) ? getSSLSocketFactory() : SocketFactory.getDefault();
                mSocket = factory.createSocket(mURI.getHost(), port);

                PrintWriter out = new PrintWriter(mSocket.getOutputStream());
                out.print("GET " + path + " HTTP/1.1\r\n");
                out.print("Upgrade: websocket\r\n");
                out.print("Connection: Upgrade\r\n");
                out.print("Host: " + mURI.getHost()+":"+mURI.getPort() + "\r\n");
                out.print("Origin: " + origin.toString() + "\r\n");
                out.print("Sec-WebSocket-Key: " + createSecret() + "\r\n");
                out.print("Sec-WebSocket-Version: 13\r\n");
                if (mExtraHeaders != null) {
                    for (NameValuePair pair : mExtraHeaders) {
                        out.print(String.format("%s: %s\r\n", pair.getName(), pair.getValue()));
                    }
                }
                out.print("\r\n");
                out.flush();

                HybiParser.HappyDataInputStream stream = new HybiParser.HappyDataInputStream(mSocket.getInputStream());

                // Read HTTP response status line.
                StatusLine statusLine = parseStatusLine(readLine(stream));
                if (statusLine == null) {
                    throw new HttpException("Received no reply from server.");
                } else if (statusLine.getStatusCode() != HttpStatus.SC_SWITCHING_PROTOCOLS) {
                    throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
                }

                // Read HTTP response headers.
                String line;
                while (!TextUtils.isEmpty(line = readLine(stream))) {
                    Header header = parseHeader(line);
                    if (header.getName().equals("Sec-WebSocket-Accept")) {
                        // FIXME: Verify the response...
                    }
                }

                mListener.onConnect();

                mConnected = true;

                // Now decode websocket frames.
                mParser.start(stream);

            } catch (EOFException ex) {
                Log.d(TAG, "WebSocket EOF!", ex);
                mListener.onDisconnect(0, "EOF");
                mConnected = false;

            } catch (SSLException ex) {
                // Connection reset by peer
                Log.d(TAG, "Websocket SSL error!", ex);
                mListener.onDisconnect(0, "SSL");
                mConnected = false;

            } catch (Exception ex) {
                mListener.onError(ex);
            }
        }
    });
    mThread.start();
}
 
源代码3 项目: Asqatasun   文件: HttpRequestHandler.java
private int computeStatus(int status) {
    switch (status) { 
        case HttpStatus.SC_FORBIDDEN:
        case HttpStatus.SC_METHOD_NOT_ALLOWED:
        case HttpStatus.SC_BAD_REQUEST:
        case HttpStatus.SC_UNAUTHORIZED:
        case HttpStatus.SC_PAYMENT_REQUIRED:
        case HttpStatus.SC_NOT_FOUND:
        case HttpStatus.SC_NOT_ACCEPTABLE:
        case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
        case HttpStatus.SC_REQUEST_TIMEOUT:
        case HttpStatus.SC_CONFLICT:
        case HttpStatus.SC_GONE:
        case HttpStatus.SC_LENGTH_REQUIRED:
        case HttpStatus.SC_PRECONDITION_FAILED:
        case HttpStatus.SC_REQUEST_TOO_LONG:
        case HttpStatus.SC_REQUEST_URI_TOO_LONG:
        case HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE:
        case HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE:
        case HttpStatus.SC_EXPECTATION_FAILED:
        case HttpStatus.SC_INSUFFICIENT_SPACE_ON_RESOURCE:
        case HttpStatus.SC_METHOD_FAILURE:
        case HttpStatus.SC_UNPROCESSABLE_ENTITY:
        case HttpStatus.SC_LOCKED:
        case HttpStatus.SC_FAILED_DEPENDENCY:
        case HttpStatus.SC_INTERNAL_SERVER_ERROR:
        case HttpStatus.SC_NOT_IMPLEMENTED:
        case HttpStatus.SC_BAD_GATEWAY:
        case HttpStatus.SC_SERVICE_UNAVAILABLE:
        case HttpStatus.SC_GATEWAY_TIMEOUT:
        case HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED:
        case HttpStatus.SC_INSUFFICIENT_STORAGE:
            return 0;
        case HttpStatus.SC_CONTINUE:
        case HttpStatus.SC_SWITCHING_PROTOCOLS:
        case HttpStatus.SC_PROCESSING:
        case HttpStatus.SC_OK:
        case HttpStatus.SC_CREATED:
        case HttpStatus.SC_ACCEPTED:
        case HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION:
        case HttpStatus.SC_NO_CONTENT:
        case HttpStatus.SC_RESET_CONTENT:
        case HttpStatus.SC_PARTIAL_CONTENT:
        case HttpStatus.SC_MULTI_STATUS:
        case HttpStatus.SC_MULTIPLE_CHOICES:
        case HttpStatus.SC_MOVED_PERMANENTLY:
        case HttpStatus.SC_MOVED_TEMPORARILY:
        case HttpStatus.SC_SEE_OTHER:
        case HttpStatus.SC_NOT_MODIFIED:
        case HttpStatus.SC_USE_PROXY:
        case HttpStatus.SC_TEMPORARY_REDIRECT:
            return 1;
        default : 
            return 1;
    }
}