コード例 #1
0
ファイル: HttpRequest.java プロジェクト: zhangwei5095/jodd
  /**
   * Continues using the same keep-alive connection. Don't use any variant of <code>open()</code>
   * when continuing the communication! First it checks if "Connection" header exist in the response
   * and if it is equal to "Keep-Alive" value. Then it checks the "Keep-Alive" headers "max"
   * parameter. If its value is positive, then the existing {@link jodd.http.HttpConnection} from
   * the request will be reused. If max value is 1, connection will be sent with "Connection: Close"
   * header, indicating its the last request. When new connection is created, the same {@link
   * jodd.http.HttpConnectionProvider} that was used for creating initial connection is used for
   * opening the new connection.
   *
   * @param doContinue set it to <code>false</code> to indicate the last connection
   */
  public HttpRequest keepAlive(HttpResponse httpResponse, boolean doContinue) {
    boolean keepAlive = httpResponse.isConnectionPersistent();
    if (keepAlive) {
      HttpConnection previousConnection = httpResponse.getHttpRequest().httpConnection;

      if (previousConnection != null) {
        // keep using the connection!
        this.httpConnection = previousConnection;
        this.httpConnectionProvider = httpResponse.getHttpRequest().connectionProvider();
      }

      // keepAlive = true; (already set)
    } else {
      // close previous connection
      httpResponse.close();

      // force keep-alive on new request
      keepAlive = true;
    }

    // if we don't want to continue with this persistent session, mark this connection as closed
    if (!doContinue) {
      keepAlive = false;
    }

    connectionKeepAlive(keepAlive);

    // if connection is not opened, open it using previous connection provider
    if (httpConnection == null) {
      open(httpResponse.getHttpRequest().connectionProvider());
    }
    return this;
  }
コード例 #2
0
  private void sendResponse(HttpResponse response) {
    Log.i(LOG_TAG, " sendResponse() started. Response = " + response);

    try {
      OutputStream out = socket.getOutputStream();

      response.write(out);

      out.flush();
      out.close();
      response.close();
    } catch (IOException ioe) {
      Log.e(LOG_TAG, "IOException writing to socket.", ioe);
    }

    Log.i(LOG_TAG, " sendResponse() ended.");
  }