本文整理了Java中java.net.URLConnection.setConnectTimeout()
方法的一些代码示例,展示了URLConnection.setConnectTimeout()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。URLConnection.setConnectTimeout()
方法的具体详情如下:
包路径:java.net.URLConnection
类名称:URLConnection
方法名:setConnectTimeout
[英]Sets the maximum time in milliseconds to wait while connecting. Connecting to a server will fail with a SocketTimeoutException if the timeout elapses before a connection is established. The default value of 0 causes us to do a blocking connect. This does not mean we will never time out, but it probably means you'll get a TCP timeout after several minutes.
Warning: if the hostname resolves to multiple IP addresses, this client will try each in RFC 3484 order. If connecting to each of these addresses fails, multiple timeouts will elapse before the connect attempt throws an exception. Host names that support both IPv6 and IPv4 always have at least 2 IP addresses.
[中]设置连接时等待的最长时间(以毫秒为单位)。如果在建立连接之前超时,连接到服务器将失败,并出现SocketTimeoutException。默认值0导致我们进行阻塞连接。这并不意味着我们永远不会超时,但它可能意味着您将在几分钟后获得TCP超时。
警告:如果主机名解析为多个IP地址,此客户端将按RFC 3484顺序尝试每个IP地址。如果连接到这些地址中的每一个都失败,则在连接尝试引发异常之前,将经过多次超时。同时支持IPv6和IPv4的主机名始终至少有2个IP地址。
代码示例来源:origin: stackoverflow.com
URL url = new URL(urlPath);
URLConnection con = url.openConnection();
con.setConnectTimeout(connectTimeout);
con.setReadTimeout(readTimeout);
InputStream in = con.getInputStream();
代码示例来源:origin: lingochamp/FileDownloader
public FileDownloadUrlConnection(URL url, Configuration configuration) throws IOException {
if (configuration != null && configuration.proxy != null) {
mConnection = url.openConnection(configuration.proxy);
} else {
mConnection = url.openConnection();
}
if (configuration != null) {
if (configuration.readTimeout != null) {
mConnection.setReadTimeout(configuration.readTimeout);
}
if (configuration.connectTimeout != null) {
mConnection.setConnectTimeout(configuration.connectTimeout);
}
}
}
代码示例来源:origin: stackoverflow.com
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setRequestMethod("HEAD");
con.setConnectTimeout(5000); //set timeout to 5 seconds
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (java.net.SocketTimeoutException e) {
return false;
} catch (java.io.IOException e) {
return false;
}
代码示例来源:origin: jenkinsci/jenkins
/**
* This method should be used wherever {@link URL#openConnection()} to internet URLs is invoked directly.
*/
public static URLConnection open(URL url) throws IOException {
final ProxyConfiguration p = get();
URLConnection con;
if(p==null) {
con = url.openConnection();
} else {
Proxy proxy = p.createProxy(url.getHost());
con = url.openConnection(proxy);
if(p.getUserName()!=null) {
// Add an authenticator which provides the credentials for proxy authentication
Authenticator.setDefault(p.authenticator);
p.jenkins48775workaround(proxy, url);
}
}
if(DEFAULT_CONNECT_TIMEOUT_MILLIS > 0) {
con.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLIS);
}
if (JenkinsJVM.isJenkinsJVM()) { // this code may run on a slave
decorate(con);
}
return con;
}
代码示例来源:origin: rhuss/jolokia
@Override
public Result authenticate(HttpExchange pHttpExchange) {
try {
URLConnection connection = delegateURL.openConnection();
connection.addRequestProperty("Authorization",
pHttpExchange.getRequestHeaders().getFirst("Authorization"));
connection.setConnectTimeout(2000);
connection.connect();
if (connection instanceof HttpURLConnection) {
HttpURLConnection httpConnection = (HttpURLConnection) connection;
return httpConnection.getResponseCode() == 200 ?
new Success(principalExtractor.extract(connection)) :
new Failure(401);
} else {
return new Failure(401);
}
} catch (final IOException e) {
return prepareFailure(pHttpExchange, "Cannot call delegate url " + delegateURL + ": " + e, 503);
} catch (final IllegalArgumentException e) {
return prepareFailure(pHttpExchange, "Illegal Argument: " + e, 400);
} catch (ParseException e) {
return prepareFailure(pHttpExchange, "Invalid JSON response: " + e, 422);
}
}
代码示例来源:origin: stackoverflow.com
System.setProperty("http.keepAlive", "false");
HttpURLConnection conn = (HttpURLConnection) mURL.openConnection();
conn.setUseCaches(false);
conn.setRequestProperty("User-Agent", useragent);
conn.setConnectTimeout(30000);
conn.setDoOutput(true);
conn.setDoInput(true);
consumer.sign(conn);
conn.connect();
InputSource is = new InputSource(conn.getInputStream());
代码示例来源:origin: osmandapp/Osmand
if (request.referer != null)
connection.setRequestProperty("Referer", request.referer); //$NON-NLS-1$
connection.setConnectTimeout(CONNECTION_TIMEOUT);
connection.setReadTimeout(CONNECTION_TIMEOUT);
BufferedInputStream inputStream = new BufferedInputStream(connection.getInputStream(), 8 * 1024);
request.saveTile(inputStream);
if (log.isDebugEnabled()) {
代码示例来源:origin: lingochamp/FileDownloader
@Test
public void construct_noConfiguration_noAssigned() throws IOException {
FileDownloadUrlConnection.Creator creator = new FileDownloadUrlConnection.Creator();
creator.create("http://blog.dreamtobe.cn");
verify(mConnection, times(0)).setConnectTimeout(anyInt());
verify(mConnection, times(0)).setReadTimeout(anyInt());
}
代码示例来源:origin: commons-io/commons-io
/**
* Copies bytes from the URL <code>source</code> to a file
* <code>destination</code>. The directories up to <code>destination</code>
* will be created if they don't already exist. <code>destination</code>
* will be overwritten if it already exists.
*
* @param source the <code>URL</code> to copy bytes from, must not be {@code null}
* @param destination the non-directory <code>File</code> to write bytes to
* (possibly overwriting), must not be {@code null}
* @param connectionTimeout the number of milliseconds until this method
* will timeout if no connection could be established to the <code>source</code>
* @param readTimeout the number of milliseconds until this method will
* timeout if no data could be read from the <code>source</code>
* @throws IOException if <code>source</code> URL cannot be opened
* @throws IOException if <code>destination</code> is a directory
* @throws IOException if <code>destination</code> cannot be written
* @throws IOException if <code>destination</code> needs creating but can't be
* @throws IOException if an IO error occurs during copying
* @since 2.0
*/
public static void copyURLToFile(final URL source, final File destination,
final int connectionTimeout, final int readTimeout) throws IOException {
final URLConnection connection = source.openConnection();
connection.setConnectTimeout(connectionTimeout);
connection.setReadTimeout(readTimeout);
copyInputStreamToFile(connection.getInputStream(), destination);
}
代码示例来源:origin: alibaba/mdrill
private URLConnection getConnection(String urlString) throws IOException {
URL connURL = new URL(urlString);
URLConnection conn = null;
// setup the proxy (if we are using one) and open the connect
if (setProxyAuthentication()) {
conn = connURL.openConnection(m_httpProxy);
} else {
conn = connURL.openConnection();
}
// Set a timeout of 60 seconds for establishing the connection
conn.setConnectTimeout(60000);
return conn;
}
代码示例来源:origin: lingochamp/okdownload
void configUrlConnection() throws IOException {
Util.d(TAG, "config connection for " + url);
if (configuration != null && configuration.proxy != null) {
connection = url.openConnection(configuration.proxy);
} else {
connection = url.openConnection();
}
if (configuration != null) {
if (configuration.readTimeout != null) {
connection.setReadTimeout(configuration.readTimeout);
}
if (configuration.connectTimeout != null) {
connection.setConnectTimeout(configuration.connectTimeout);
}
}
}
代码示例来源:origin: stackoverflow.com
URLConnection conn = url.openConnection();
conn.setConnectTimeout(timeoutMs);
conn.setReadTimeout(timeoutMs);
in = conn.getInputStream();
代码示例来源:origin: lingochamp/okdownload
@Test
public void construct_noConfiguration_noAssigned() throws IOException {
DownloadUrlConnection.Factory factory = new DownloadUrlConnection.Factory();
factory.create("https://jacksgong.com");
verify(urlConnection, never()).setConnectTimeout(anyInt());
verify(urlConnection, never()).setReadTimeout(anyInt());
}
代码示例来源:origin: stackoverflow.com
public static boolean hasActiveInternetConnection(Context context) {
if (isNetworkAvailable(context)) {
try {
HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
return (urlc.getResponseCode() == 200);
} catch (IOException e) {
Log.e(LOG_TAG, "Error checking internet connection", e);
}
} else {
Log.d(LOG_TAG, "No network available!");
}
return false;
}
代码示例来源:origin: stackoverflow.com
try {
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setRequestMethod("HEAD");
con.setConnectTimeout(5000); //set timeout to 5 seconds
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (java.net.SocketTimeoutException e) {
return false;
} catch (java.io.IOException e) {
return false;
}
代码示例来源:origin: javamelody/javamelody
/**
* Ouvre la connection http.
* @return Object
* @throws IOException Exception de communication
*/
private URLConnection openConnection() throws IOException {
final URLConnection connection = url.openConnection();
connection.setUseCaches(false);
if (CONNECTION_TIMEOUT > 0) {
connection.setConnectTimeout(CONNECTION_TIMEOUT);
}
if (READ_TIMEOUT > 0) {
connection.setReadTimeout(READ_TIMEOUT);
}
// grâce à cette propriété, l'application retournera un flux compressé si la taille
// dépasse x Ko
connection.setRequestProperty("Accept-Encoding", "gzip");
if (headers != null) {
for (final Map.Entry<String, String> entry : headers.entrySet()) {
connection.setRequestProperty(entry.getKey(), entry.getValue());
}
}
if (url.getUserInfo() != null) {
final String authorization = Base64Coder.encodeString(url.getUserInfo());
connection.setRequestProperty("Authorization", "Basic " + authorization);
}
return connection;
}
代码示例来源:origin: Netflix/servo
@Override
public InputStream fetchStatus() throws IOException {
final URLConnection con = url.openConnection();
con.setConnectTimeout(1000);
con.setReadTimeout(2000);
return con.getInputStream();
}
}
代码示例来源:origin: lingochamp/FileDownloader
@Test
public void construct_validConfiguration_Assigned() throws IOException {
FileDownloadUrlConnection.Creator creator = new FileDownloadUrlConnection.Creator(
new FileDownloadUrlConnection.Configuration()
.proxy(mProxy)
.connectTimeout(1001)
.readTimeout(1002)
);
creator.create(mURL);
verify(mURL).openConnection(mProxy);
verify(mConnection).setConnectTimeout(1001);
verify(mConnection).setReadTimeout(1002);
}
代码示例来源:origin: stackoverflow.com
public boolean isConnectedToServer(String url, int timeout) {
try{
URL myUrl = new URL(url);
URLConnection connection = myUrl.openConnection();
connection.setConnectTimeout(timeout);
connection.connect();
return true;
} catch (Exception e) {
// Handle your exceptions
return false;
}
}
代码示例来源:origin: lingochamp/okdownload
@Test
public void construct_validConfiguration_Assigned() throws IOException {
DownloadUrlConnection.Factory factory = new DownloadUrlConnection.Factory(
new DownloadUrlConnection.Configuration()
.proxy(proxy)
.connectTimeout(123)
.readTimeout(456)
);
factory.create(url);
verify(url).openConnection(proxy);
verify(urlConnection).setConnectTimeout(123);
verify(urlConnection).setReadTimeout(456);
}
内容来源于网络,如有侵权,请联系作者删除!