本文整理汇总了Java中org.apache.http.HttpConnection类的典型用法代码示例。如果您正苦于以下问题:Java HttpConnection类的具体用法?Java HttpConnection怎么用?Java HttpConnection使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HttpConnection类属于org.apache.http包,在下文中一共展示了HttpConnection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: closeIdleConnections
import org.apache.http.HttpConnection; //导入依赖的package包/类
/**
* Closes connections that have been idle for at least the given amount of time.
*
* @param idleTime the minimum idle time, in milliseconds, for connections to be closed
*/
public void closeIdleConnections(long idleTime) {
// the latest time for which connections will be closed
long idleTimeout = System.currentTimeMillis() - idleTime;
if (log.isDebugEnabled()) {
log.debug("Checking for connections, idle timeout: " + idleTimeout);
}
for (Entry<HttpConnection, TimeValues> entry : connectionToTimes.entrySet()) {
HttpConnection conn = entry.getKey();
TimeValues times = entry.getValue();
long connectionTime = times.timeAdded;
if (connectionTime <= idleTimeout) {
if (log.isDebugEnabled()) {
log.debug("Closing idle connection, connection time: " + connectionTime);
}
try {
conn.close();
} catch (IOException ex) {
log.debug("I/O error closing connection", ex);
}
}
}
}
示例2: closeExpiredConnections
import org.apache.http.HttpConnection; //导入依赖的package包/类
public void closeExpiredConnections() {
long now = System.currentTimeMillis();
if (log.isDebugEnabled()) {
log.debug("Checking for expired connections, now: " + now);
}
for (Entry<HttpConnection, TimeValues> entry : connectionToTimes.entrySet()) {
HttpConnection conn = entry.getKey();
TimeValues times = entry.getValue();
if(times.timeExpires <= now) {
if (log.isDebugEnabled()) {
log.debug("Closing connection, expired @: " + times.timeExpires);
}
try {
conn.close();
} catch (IOException ex) {
log.debug("I/O error closing connection", ex);
}
}
}
}
示例3: getUserToken
import org.apache.http.HttpConnection; //导入依赖的package包/类
public Object getUserToken(final HttpContext context) {
final HttpClientContext clientContext = HttpClientContext.adapt(context);
Principal userPrincipal = null;
final AuthStateHC4 targetAuthState = clientContext.getTargetAuthState();
if (targetAuthState != null) {
userPrincipal = getAuthPrincipal(targetAuthState);
if (userPrincipal == null) {
final AuthStateHC4 proxyAuthState = clientContext.getProxyAuthState();
userPrincipal = getAuthPrincipal(proxyAuthState);
}
}
if (userPrincipal == null) {
final HttpConnection conn = clientContext.getConnection();
if (conn.isOpen() && conn instanceof ManagedHttpClientConnection) {
final SSLSession sslsession = ((ManagedHttpClientConnection) conn).getSSLSession();
if (sslsession != null) {
userPrincipal = sslsession.getLocalPrincipal();
}
}
}
return userPrincipal;
}
示例4: closeIdleConnections
import org.apache.http.HttpConnection; //导入依赖的package包/类
/**
* Closes connections that have been idle for at least the given amount of time.
*
* @param idleTime the minimum idle time, in milliseconds, for connections to be closed
*/
public void closeIdleConnections(final long idleTime) {
// the latest time for which connections will be closed
final long idleTimeout = System.currentTimeMillis() - idleTime;
if (log.isDebugEnabled()) {
log.debug("Checking for connections, idle timeout: " + idleTimeout);
}
for (final Entry<HttpConnection, TimeValues> entry : connectionToTimes.entrySet()) {
final HttpConnection conn = entry.getKey();
final TimeValues times = entry.getValue();
final long connectionTime = times.timeAdded;
if (connectionTime <= idleTimeout) {
if (log.isDebugEnabled()) {
log.debug("Closing idle connection, connection time: " + connectionTime);
}
try {
conn.close();
} catch (final IOException ex) {
log.debug("I/O error closing connection", ex);
}
}
}
}
示例5: closeExpiredConnections
import org.apache.http.HttpConnection; //导入依赖的package包/类
public void closeExpiredConnections() {
final long now = System.currentTimeMillis();
if (log.isDebugEnabled()) {
log.debug("Checking for expired connections, now: " + now);
}
for (final Entry<HttpConnection, TimeValues> entry : connectionToTimes.entrySet()) {
final HttpConnection conn = entry.getKey();
final TimeValues times = entry.getValue();
if(times.timeExpires <= now) {
if (log.isDebugEnabled()) {
log.debug("Closing connection, expired @: " + times.timeExpires);
}
try {
conn.close();
} catch (final IOException ex) {
log.debug("I/O error closing connection", ex);
}
}
}
}
示例6: getUserToken
import org.apache.http.HttpConnection; //导入依赖的package包/类
@Override
public Object getUserToken(final HttpContext context) {
final HttpClientContext clientContext = HttpClientContext.adapt(context);
Principal userPrincipal = null;
final AuthState targetAuthState = clientContext.getTargetAuthState();
if (targetAuthState != null) {
userPrincipal = getAuthPrincipal(targetAuthState);
if (userPrincipal == null) {
final AuthState proxyAuthState = clientContext.getProxyAuthState();
userPrincipal = getAuthPrincipal(proxyAuthState);
}
}
if (userPrincipal == null) {
final HttpConnection conn = clientContext.getConnection();
if (conn.isOpen() && conn instanceof ManagedHttpClientConnection) {
final SSLSession sslsession = ((ManagedHttpClientConnection) conn).getSSLSession();
if (sslsession != null) {
userPrincipal = sslsession.getLocalPrincipal();
}
}
}
return userPrincipal;
}
示例7: closeExpiredConnections
import org.apache.http.HttpConnection; //导入依赖的package包/类
public void closeExpiredConnections() {
long now = System.currentTimeMillis();
if (log.isDebugEnabled()) {
log.debug("Checking for expired connections, now: " + now);
}
Iterator<HttpConnection> connectionIter =
connectionToTimes.keySet().iterator();
while (connectionIter.hasNext()) {
HttpConnection conn = connectionIter.next();
TimeValues times = connectionToTimes.get(conn);
if(times.timeExpires <= now) {
if (log.isDebugEnabled()) {
log.debug("Closing connection, expired @: " + times.timeExpires);
}
try {
conn.close();
} catch (IOException ex) {
log.debug("I/O error closing connection", ex);
}
}
}
}
示例8: add
import org.apache.http.HttpConnection; //导入依赖的package包/类
/**
* Registers the given connection with this handler. The connection will be held until
* {@link #remove} or {@link #closeIdleConnections} is called.
*
* @param connection the connection to add
*
* @see #remove
*/
public void add(HttpConnection connection, long validDuration, TimeUnit unit) {
long timeAdded = System.currentTimeMillis();
if (log.isDebugEnabled()) {
log.debug("Adding connection at: " + timeAdded);
}
connectionToTimes.put(connection, new TimeValues(timeAdded, validDuration, unit));
}
示例9: remove
import org.apache.http.HttpConnection; //导入依赖的package包/类
/**
* Removes the given connection from the list of connections to be closed when idle.
* This will return true if the connection is still valid, and false
* if the connection should be considered expired and not used.
*
* @param connection
* @return True if the connection is still valid.
*/
public boolean remove(HttpConnection connection) {
TimeValues times = connectionToTimes.remove(connection);
if(times == null) {
log.warn("Removing a connection that never existed!");
return true;
} else {
return System.currentTimeMillis() <= times.timeExpires;
}
}
示例10: process
import org.apache.http.HttpConnection; //导入依赖的package包/类
public void process(final HttpRequest request, final HttpContext context)
throws HttpException, IOException {
Args.notNull(request, "HTTP request");
final HttpCoreContext corecontext = HttpCoreContext.adapt(context);
final ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
final String method = request.getRequestLine().getMethod();
if (method.equalsIgnoreCase("CONNECT") && ver.lessEquals(HttpVersion.HTTP_1_0)) {
return;
}
if (!request.containsHeader(HTTP.TARGET_HOST)) {
HttpHost targethost = corecontext.getTargetHost();
if (targethost == null) {
final HttpConnection conn = corecontext.getConnection();
if (conn instanceof HttpInetConnection) {
// Populate the context with a default HTTP host based on the
// inet address of the target host
final InetAddress address = ((HttpInetConnection) conn).getRemoteAddress();
final int port = ((HttpInetConnection) conn).getRemotePort();
if (address != null) {
targethost = new HttpHost(address.getHostName(), port);
}
}
if (targethost == null) {
if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
return;
} else {
throw new ProtocolException("Target host missing");
}
}
}
request.addHeader(HTTP.TARGET_HOST, targethost.toHostString());
}
}
示例11: add
import org.apache.http.HttpConnection; //导入依赖的package包/类
/**
* Registers the given connection with this handler. The connection will be held until
* {@link #remove} or {@link #closeIdleConnections} is called.
*
* @param connection the connection to add
*
* @see #remove
*/
public void add(final HttpConnection connection, final long validDuration, final TimeUnit unit) {
final long timeAdded = System.currentTimeMillis();
if (log.isDebugEnabled()) {
log.debug("Adding connection at: " + timeAdded);
}
connectionToTimes.put(connection, new TimeValues(timeAdded, validDuration, unit));
}
示例12: remove
import org.apache.http.HttpConnection; //导入依赖的package包/类
/**
* Removes the given connection from the list of connections to be closed when idle.
* This will return true if the connection is still valid, and false
* if the connection should be considered expired and not used.
*
* @param connection
* @return True if the connection is still valid.
*/
public boolean remove(final HttpConnection connection) {
final TimeValues times = connectionToTimes.remove(connection);
if(times == null) {
log.warn("Removing a connection that never existed!");
return true;
} else {
return System.currentTimeMillis() <= times.timeExpires;
}
}
示例13: abort
import org.apache.http.HttpConnection; //导入依赖的package包/类
public void abort() {
request.abort();
// try to close the connection? is this necessary? unclear based on preliminary debugging of HttpClient, but
// it doesn't seem to hurt to try
HttpConnection conn = (HttpConnection) ctx.getAttribute("http.connection");
if (conn != null) {
try {
conn.close();
} catch (IOException e) {
// this is fine, we're shutting it down anyway
}
}
}
示例14: add
import org.apache.http.HttpConnection; //导入依赖的package包/类
/**
* Registers the given connection with this handler. The connection will be held until
* {@link #remove} or {@link #closeIdleConnections} is called.
*
* @param connection the connection to add
*
* @see #remove
*/
public void add(HttpConnection connection, long validDuration, TimeUnit unit) {
long timeAdded = System.currentTimeMillis();
if (log.isDebugEnabled()) {
log.debug("Adding connection at: " + timeAdded);
}
connectionToTimes.put(connection, new TimeValues(timeAdded, validDuration, unit));
}
示例15: remove
import org.apache.http.HttpConnection; //导入依赖的package包/类
/**
* Removes the given connection from the list of connections to be closed when idle.
* This will return true if the connection is still valid, and false
* if the connection should be considered expired and not used.
*
* @param connection
* @return True if the connection is still valid.
*/
public boolean remove(HttpConnection connection) {
TimeValues times = connectionToTimes.remove(connection);
if(times == null) {
log.warn("Removing a connection that never existed!");
return true;
} else {
return System.currentTimeMillis() <= times.timeExpires;
}
}