org.apache.commons.httpclient.HttpConnection类的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(10.0k)|赞(0)|评价(0)|浏览(148)

本文整理了Java中org.apache.commons.httpclient.HttpConnection类的一些代码示例,展示了HttpConnection类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpConnection类的具体详情如下:
包路径:org.apache.commons.httpclient.HttpConnection
类名称:HttpConnection

HttpConnection介绍

[英]An abstraction of an HTTP InputStream and OutputStreampair, together with the relevant attributes.

The following options are set on the socket before getting the input/output streams in the #open() method:

Socket Method Sockets Option Configuration java.net.Socket#setTcpNoDelay(boolean)SO_NODELAY HttpConnectionParams#setTcpNoDelay(boolean)java.net.Socket#setSoTimeout(int)SO_TIMEOUT HttpConnectionParams#setSoTimeout(int)java.net.Socket#setSendBufferSize(int)SO_SNDBUF HttpConnectionParams#setSendBufferSize(int)java.net.Socket#setReceiveBufferSize(int)SO_RCVBUF HttpConnectionParams#setReceiveBufferSize(int)
[中]HTTP InputStream和OutputStreampair以及相关属性的抽象。
在#open()方法中获取输入/输出流之前,在套接字上设置以下选项:
套接字方法套接字选项配置java。网Socket#setTcpNoDelay(布尔值)SO#NODELAY HttpConnectionParams#setTcpNoDelay(布尔值)java。网Socket#setSoTimeout(int)SO#超时HttpConnectionParams#setSoTimeout(int)java。网Socket#setSendBufferSize(int)SO#SNDBUF HttpConnectionParams#setSendBufferSize(int)java。网套接字#setReceiveBufferSize(int)SO#RCVBUF HttpConnectionParams#setReceiveBufferSize(int)

代码示例

代码示例来源:origin: commons-httpclient/commons-httpclient

httpConnection = new HttpConnection(hostConfiguration);
  httpConnection.setHttpConnectionManager(this);
  httpConnection.getParams().setDefaults(this.params);
} else {
    || !hostConfiguration.proxyEquals(httpConnection)) {
    if (httpConnection.isOpen()) {
      httpConnection.close();
    httpConnection.setHost(hostConfiguration.getHost());
    httpConnection.setPort(hostConfiguration.getPort());
    httpConnection.setProtocol(hostConfiguration.getProtocol());
    httpConnection.setLocalAddress(hostConfiguration.getLocalAddress());
    httpConnection.setProxyHost(hostConfiguration.getProxyHost());
    httpConnection.setProxyPort(hostConfiguration.getProxyPort());
  } else {
    finishLastResponse(httpConnection);

代码示例来源:origin: commons-httpclient/commons-httpclient

responseConnection.setLastResponseInputStream(null);
  if (shouldCloseConnection(responseConnection)) {
    responseConnection.close();
  } else {
    try {
      if(responseConnection.isResponseAvailable()) {
        boolean logExtraInput =
          getParams().isParameterTrue(HttpMethodParams.WARN_EXTRA_INPUT);
        responseConnection.close();
      responseConnection.close();
ensureConnectionRelease();

代码示例来源:origin: commons-httpclient/commons-httpclient

/**
 * Since the same connection is about to be reused, make sure the
 * previous request was completely processed, and if not
 * consume it now.
 * @param conn The connection
 */
static void finishLastResponse(HttpConnection conn) {
  InputStream lastResponse = conn.getLastResponseInputStream();
  if (lastResponse != null) {
    conn.setLastResponseInputStream(null);
    try {
      lastResponse.close();
    } catch (IOException ioe) {
      conn.close();
    }
  }
}

代码示例来源:origin: commons-httpclient/commons-httpclient

public HttpConnection getConnectionWithTimeout(
  HostConfiguration hostConfiguration, long timeout) {
  httpConnection = new HttpConnection(hostConfiguration);
  httpConnection.setHttpConnectionManager(this);
  httpConnection.getParams().setDefaults(connectionParams);
  return httpConnection;
}

代码示例来源:origin: commons-httpclient/commons-httpclient

/**
 * Closes the connection if stale.
 * 
 * @return <code>true</code> if the connection was stale and therefore closed, 
 * <code>false</code> otherwise.
 * 
 * @see #isStale()
 * 
 * @since 3.0
 */
public boolean closeIfStale() throws IOException {
  if (isOpen && isStale()) {
    LOG.debug("Connection is stale, closing...");
    close();
    return true;
  }
  return false;
}

代码示例来源:origin: commons-httpclient/commons-httpclient

LOG.trace(
  "enter HttpMethodBase.writeRequest(HttpState, HttpConnection)");
writeRequestLine(state, conn);
writeRequestHeaders(state, conn);
conn.writeLine(); // close head
if (Wire.HEADER_WIRE.enabled()) {
  Wire.HEADER_WIRE.output("\r\n");
    conn.flushRequestOutputStream();
    int readTimeout = conn.getParams().getSoTimeout();
    try {
      conn.setSocketTimeout(RESPONSE_WAIT_TIME_MS);
      readStatusLine(state, conn);
      processStatusLine(state, conn);
      LOG.info("100 (continue) read timeout. Resume sending the request");
    } finally {
      conn.setSocketTimeout(readTimeout);
writeRequestBody(state, conn);
conn.flushRequestOutputStream();

代码示例来源:origin: commons-httpclient/commons-httpclient

InputStream is = conn.getResponseInputStream();
if (Wire.CONTENT_WIRE.enabled()) {
  is = new WireLogInputStream(is, Wire.CONTENT_WIRE);
boolean canHaveBody = canResponseHaveBody(statusLine.getStatusCode());
InputStream result = null;
Header transferEncodingHeader = responseHeaders.getFirstHeader("Transfer-Encoding");
  String transferEncoding = transferEncodingHeader.getValue();
  if (!"chunked".equalsIgnoreCase(transferEncoding) 
    && !"identity".equalsIgnoreCase(transferEncoding)) {
  HeaderElement[] encodings = transferEncodingHeader.getElements();
  if ((len > 0) && ("chunked".equalsIgnoreCase(encodings[len - 1].getName()))) { 
    if (conn.isResponseAvailable(conn.getParams().getSoTimeout())) {
      result = new ChunkedInputStream(is, this);
    } else {
      if (getParams().isParameterTrue(HttpMethodParams.STRICT_TRANSFER_ENCODING)) {
        throw new ProtocolException("Chunk-encoded body declared but not sent");
      } else {
    setConnectionCloseForced(true);
    result = is;

代码示例来源:origin: slide/slide-webdavlib

public boolean unsubscribe(String uri, Subscriber listener, Credentials credentials) {
  UnsubscribeMethod unsubscribeMethod = new UnsubscribeMethod(repositoryDomain+uri);
  synchronized ( subscribers ) {
    for ( Iterator i = subscribers.iterator(); i.hasNext(); ) {
      Subscription subscription = (Subscription)i.next();
      if ( subscription.getSubscriber().equals(listener) ) {
        String id = String.valueOf(subscription.getId());
        unsubscribeMethod.addRequestHeader(UnsubscribeMethod.H_SUBSCRIPTION_ID, id);
        try {
          unsubscribeMethod.setDoAuthentication(true);
          HttpState httpState = new HttpState();
          httpState.setCredentials(null, repositoryHost, credentials);
          HttpConnection httpConnection = new HttpConnection(repositoryHost, repositoryPort, protocol);
          httpConnection.setConnectionTimeout(CONNECTION_TIMEOUT);
          int state = unsubscribeMethod.execute(httpState, httpConnection);
          if ( state == HttpStatus.SC_OK ) {
            i.remove();
            return true;
          } else {
            logger.log(Level.SEVERE, "Unsubscription failed. State: "+state);
          }
        } catch (IOException e) {
          logger.log(Level.SEVERE, "Unsubscription of listener '"+listener+"' failed!", e);
        }
      }
    }
  }
  logger.log(Level.SEVERE, "Listener not unsubscribed!");
  return false;
}

代码示例来源:origin: slide/slide-webdavlib

protected void fireEvent(EventMethod eventMethod, Credentials credentials) throws IOException {
  eventMethod.setDoAuthentication(true);
  HttpState httpState = new HttpState();
  httpState.setCredentials(null, repositoryHost, credentials);
  int state = eventMethod.execute(httpState, new HttpConnection(repositoryHost, repositoryPort, protocol));
  if ( state == HttpStatus.SC_OK ) {
  } else {
    logger.log(Level.SEVERE, "Event failed. State: "+state);
  }
}

代码示例来源:origin: commons-httpclient/commons-httpclient

this.connectMethod.getParams().setDefaults(this.hostConfiguration.getParams());
  if (!this.conn.isOpen()) {
    this.conn.open();
      || this.state.isAuthenticationPreemptive()) {
    LOG.debug("Preemptively sending default basic credentials");
    this.connectMethod.getProxyAuthState().setPreemptive();
  this.conn.tunnelCreated();
  this.conn.close();
  return false;

代码示例来源:origin: commons-httpclient/commons-httpclient

LOG.trace("Attempt number " + execCount + " to process request");
    if (this.conn.getParams().isStaleCheckingEnabled()) {
      this.conn.closeIfStale();
    if (!this.conn.isOpen()) {
      this.conn.open();
      if (this.conn.isProxied() && this.conn.isSecure() 
      && !(method instanceof ConnectMethod)) {
  } catch (IOException e) {
    LOG.debug("Closing the connection.");
    this.conn.close();
        ((HttpMethodBase)method).getMethodRetryHandler();
      if (handler != null) {
        if (!handler.retryMethod(
if (this.conn.isOpen()) {
  LOG.debug("Closing the connection.");
  this.conn.close();
if (this.conn.isOpen()) {
  LOG.debug("Closing the connection.");
  this.conn.close();

代码示例来源:origin: commons-httpclient/commons-httpclient

checkExecuteConditions(state, conn);
this.statusLine = null;
this.connectionCloseForced = false;
conn.setLastResponseInputStream(null);
  this.effectiveVersion = this.params.getVersion(); 
writeRequest(state, conn);
this.requestSent = true;
readResponse(state, conn);

代码示例来源:origin: commons-httpclient/commons-httpclient

/**
 * Applies connection parameters specified for a given method
 * 
 * @param method HTTP method
 * 
 * @throws IOException if an I/O occurs setting connection parameters 
 */
private void applyConnectionParams(final HttpMethod method) throws IOException {
  int timeout = 0;
  // see if a timeout is given for this method
  Object param = method.getParams().getParameter(HttpMethodParams.SO_TIMEOUT);
  if (param == null) {
    // if not, use the default value
    param = this.conn.getParams().getParameter(HttpConnectionParams.SO_TIMEOUT);
  }
  if (param != null) {
    timeout = ((Integer)param).intValue();
  }
  this.conn.setSocketTimeout(timeout);                    
}

代码示例来源:origin: elastic/elasticsearch-hadoop

public void close() {
    if (httpConnection != null) {
      if (httpConnection.isOpen()) {
        releaseConnection(httpConnection);
      }
      httpConnection.close();
    }
    httpConnection = null;
    conn = null;
  }
}

代码示例来源:origin: commons-httpclient/commons-httpclient

/**
 * since 3.1
 */
public void shutdown() {
  httpConnection.close();
}

代码示例来源:origin: commons-httpclient/commons-httpclient

connection.close();
connection.setHttpConnectionManager(null);
connection.releaseConnection();

代码示例来源:origin: commons-httpclient/commons-httpclient

InputStream stream = readResponseBody(conn);
if (stream == null) {
  responseBodyConsumed();
} else {
  conn.setLastResponseInputStream(stream);
  setResponseStream(stream);

代码示例来源:origin: commons-httpclient/commons-httpclient

method.getParams().setDefaults(getParams());
  response.setSocket(connectionManager.getConnection().getSocket());
} else {
  connectionManager.getConnection().close();

代码示例来源:origin: commons-httpclient/commons-httpclient

public boolean isOpen() {
  if (hasConnection()) {
    return wrappedConnection.isOpen();
  } else {
    return false;
  }
}

代码示例来源:origin: commons-httpclient/commons-httpclient

public void setLastResponseInputStream(InputStream inStream) {
  if (hasConnection()) {
    wrappedConnection.setLastResponseInputStream(inStream);
  } else {
    // do nothing
  }
}

相关文章

微信公众号

最新文章

更多

HttpConnection类方法