本文整理了Java中java.net.HttpURLConnection.disconnect()
方法的一些代码示例,展示了HttpURLConnection.disconnect()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpURLConnection.disconnect()
方法的具体详情如下:
包路径:java.net.HttpURLConnection
类名称:HttpURLConnection
方法名:disconnect
[英]Releases this connection so that its resources may be either reused or closed.
Unlike other Java implementations, this will not necessarily close socket connections that can be reused. You can disable all connection reuse by setting the http.keepAlive system property to false before issuing any HTTP requests.
[中]释放此连接,以便可以重用或关闭其资源。
与其他Java实现不同,这不一定会关闭可重用的套接字连接。您可以通过设置http来禁用所有连接重用。在发出任何HTTP请求之前,将系统属性keepAlive设置为false。
代码示例来源:origin: stackoverflow.com
HttpURLConnection connection = null;
try {
URL url = new URL(sUrl[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return "Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage();
input = connection.getInputStream();
output = new FileOutputStream("/sdcard/file_name.extension");
connection.disconnect();
代码示例来源:origin: spring-projects/spring-framework
/**
* This implementation opens an InputStream for the given URL.
* <p>It sets the {@code useCaches} flag to {@code false},
* mainly to avoid jar file locking on Windows.
* @see java.net.URL#openConnection()
* @see java.net.URLConnection#setUseCaches(boolean)
* @see java.net.URLConnection#getInputStream()
*/
@Override
public InputStream getInputStream() throws IOException {
URLConnection con = this.url.openConnection();
ResourceUtils.useCachesIfNecessary(con);
try {
return con.getInputStream();
}
catch (IOException ex) {
// Close the HTTP connection (if applicable).
if (con instanceof HttpURLConnection) {
((HttpURLConnection) con).disconnect();
}
throw ex;
}
}
代码示例来源:origin: stackoverflow.com
future = executor.submit(new Callable<MyResult>() {
@Override
public MyResult call() throws IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
try {
// .. use the connection ...
} finally {
connection.disconnect();
}
}
});
MyResult result = future.get(TIMEOUT, TimeUnit.SECONDS);
代码示例来源:origin: Atmosphere/atmosphere
logger.debug("Retrieving Atmosphere's latest version from http://async-io.org/version.html");
HttpURLConnection urlConnection = (HttpURLConnection)
URI.create("http://async-io.org/version.html").toURL().openConnection();
urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0");
urlConnection.setRequestProperty("Connection", "keep-alive");
urlConnection.setInstanceFollowRedirects(true);
BufferedReader in = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream()));
} catch (IOException ex) {
urlConnection.disconnect();
代码示例来源:origin: spring-projects/spring-framework
URLConnection con = url.openConnection();
customizeConnection(con);
if (con instanceof HttpURLConnection) {
HttpURLConnection httpCon = (HttpURLConnection) con;
int code = httpCon.getResponseCode();
if (code != HttpURLConnection.HTTP_OK) {
httpCon.disconnect();
return false;
代码示例来源:origin: Atmosphere/atmosphere
public void request(String urlString) {
HttpURLConnection urlConnection = null;
try {
URL url = new URL(urlString);
urlConnection = openURLConnection(url);
urlConnection.setInstanceFollowRedirects(true);
urlConnection.setRequestMethod(GET_METHOD_NAME);
urlConnection.setRequestProperty("User-agent", uaName + " (" + osString + ")");
logger.debug("Sending Server's information to Atmosphere's Google Analytics {} {}", urlString, uaName + " (" + osString + ")");
urlConnection.connect();
int responseCode = getResponseCode(urlConnection);
if (responseCode != HttpURLConnection.HTTP_OK) {
logError("JGoogleAnalytics: Error tracking, url=" + urlString);
} else {
logMessage(SUCCESS_MESSAGE);
}
} catch (Exception e) {
logError(e.getMessage());
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
}
代码示例来源:origin: google/ExoPlayer
URL url = new URL(dataSpec.uri.toString());
@HttpMethod int httpMethod = dataSpec.httpMethod;
byte[] httpBody = dataSpec.httpBody;
makeConnection(
url, httpMethod, httpBody, position, length, allowGzip, false /* followRedirects */);
int responseCode = connection.getResponseCode();
String location = connection.getHeaderField("Location");
if ((httpMethod == DataSpec.HTTP_METHOD_GET || httpMethod == DataSpec.HTTP_METHOD_HEAD)
|| responseCode == HTTP_STATUS_TEMPORARY_REDIRECT
|| responseCode == HTTP_STATUS_PERMANENT_REDIRECT)) {
connection.disconnect();
url = handleRedirect(url, location);
} else if (httpMethod == DataSpec.HTTP_METHOD_POST
|| responseCode == HttpURLConnection.HTTP_SEE_OTHER)) {
connection.disconnect();
httpMethod = DataSpec.HTTP_METHOD_GET;
httpBody = null;
代码示例来源:origin: stackoverflow.com
URL url = new URL("http://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
finally {
urlConnection.disconnect();
}
}
代码示例来源:origin: google/agera
@Override
@NonNull
public Result<HttpResponse> apply(@NonNull final HttpRequest request) {
try {
final HttpURLConnection connection =
(HttpURLConnection) new URL(request.url).openConnection();
try {
return success(getHttpResponseResult(request, connection));
} finally {
connection.disconnect();
}
} catch (final IOException exception) {
return failure(exception);
}
}
代码示例来源:origin: spring-projects/spring-framework
URLConnection con = url.openConnection();
customizeConnection(con);
HttpURLConnection httpCon =
(con instanceof HttpURLConnection ? (HttpURLConnection) con : null);
if (httpCon != null) {
int code = httpCon.getResponseCode();
if (code == HttpURLConnection.HTTP_OK) {
return true;
httpCon.disconnect();
return false;
代码示例来源:origin: micronaut-projects/micronaut-core
@Nonnull
@Override
public InputStream asInputStream() throws IOException {
URLConnection con = this.url.openConnection();
con.setUseCaches(true);
try {
return con.getInputStream();
} catch (IOException ex) {
if (con instanceof HttpURLConnection) {
((HttpURLConnection) con).disconnect();
}
throw ex;
}
}
代码示例来源:origin: stackoverflow.com
String uri =
"http://localhost:8080/CustomerService/rest/customers/1";
URL url = new URL(uri);
HttpURLConnection connection =
(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/xml");
JAXBContext jc = JAXBContext.newInstance(Customer.class);
InputStream xml = connection.getInputStream();
Customer customer =
(Customer) jc.createUnmarshaller().unmarshal(xml);
connection.disconnect();
代码示例来源:origin: google/physical-web
urlConnection = (HttpURLConnection) mUrl.openConnection();
writeToUrlConnection(urlConnection);
responseCode = urlConnection.getResponseCode();
inputStream = new BufferedInputStream(urlConnection.getInputStream());
result = readInputStream(inputStream);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
代码示例来源:origin: thymeleaf/thymeleaf
private InputStream inputStream() throws IOException {
final URLConnection connection = this.url.openConnection();
if (connection.getClass().getSimpleName().startsWith("JNLP")) {
connection.setUseCaches(true);
}
final InputStream inputStream;
try {
inputStream = connection.getInputStream();
} catch (final IOException e) {
if (connection instanceof HttpURLConnection) {
// disconnect() will probably close the underlying socket, which is fine given we had an exception
((HttpURLConnection) connection).disconnect();
}
throw e;
}
return inputStream;
}
代码示例来源:origin: jenkinsci/jenkins
/**
* If the first URL we try to access with a HTTP proxy is HTTPS then the authentication cache will not have been
* pre-populated, so we try to access at least one HTTP URL before the very first HTTPS url.
* @param proxy
* @param url the actual URL being opened.
*/
private void jenkins48775workaround(Proxy proxy, URL url) {
if ("https".equals(url.getProtocol()) && !authCacheSeeded && proxy != Proxy.NO_PROXY) {
HttpURLConnection preAuth = null;
try {
// We do not care if there is anything at this URL, all we care is that it is using the proxy
preAuth = (HttpURLConnection) new URL("http", url.getHost(), -1, "/").openConnection(proxy);
preAuth.setRequestMethod("HEAD");
preAuth.connect();
} catch (IOException e) {
// ignore, this is just a probe we don't care at all
} finally {
if (preAuth != null) {
preAuth.disconnect();
}
}
authCacheSeeded = true;
} else if ("https".equals(url.getProtocol())){
// if we access any http url using a proxy then the auth cache will have been seeded
authCacheSeeded = authCacheSeeded || proxy != Proxy.NO_PROXY;
}
}
代码示例来源:origin: perwendel/spark
} else {
URLConnection con = url.openConnection();
customizeConnection(con);
HttpURLConnection httpCon =
(con instanceof HttpURLConnection ? (HttpURLConnection) con : null);
if (httpCon != null) {
int code = httpCon.getResponseCode();
if (code == HttpURLConnection.HTTP_OK) {
return true;
httpCon.disconnect();
return false;
} else {
代码示例来源:origin: org.springframework/spring-core
/**
* This implementation opens an InputStream for the given URL.
* <p>It sets the {@code useCaches} flag to {@code false},
* mainly to avoid jar file locking on Windows.
* @see java.net.URL#openConnection()
* @see java.net.URLConnection#setUseCaches(boolean)
* @see java.net.URLConnection#getInputStream()
*/
@Override
public InputStream getInputStream() throws IOException {
URLConnection con = this.url.openConnection();
ResourceUtils.useCachesIfNecessary(con);
try {
return con.getInputStream();
}
catch (IOException ex) {
// Close the HTTP connection (if applicable).
if (con instanceof HttpURLConnection) {
((HttpURLConnection) con).disconnect();
}
throw ex;
}
}
代码示例来源:origin: stackoverflow.com
private int tryGetFileSize(URL url) {
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("HEAD");
conn.getInputStream();
return conn.getContentLength();
} catch (IOException e) {
return -1;
} finally {
conn.disconnect();
}
}
代码示例来源:origin: org.springframework/spring-core
URLConnection con = url.openConnection();
customizeConnection(con);
if (con instanceof HttpURLConnection) {
HttpURLConnection httpCon = (HttpURLConnection) con;
int code = httpCon.getResponseCode();
if (code != HttpURLConnection.HTTP_OK) {
httpCon.disconnect();
return false;
代码示例来源:origin: micronaut-projects/micronaut-core
/**
* This implementation opens an InputStream for the given URL.
* It sets the "UseCaches" flag to <code>false</code>,
* mainly to avoid jar file locking on Windows.
*
* @return The input stream
* @throws IOException if there is an error
* @see java.net.URL#openConnection()
* @see java.net.URLConnection#setUseCaches(boolean)
* @see java.net.URLConnection#getInputStream()
*/
public InputStream getInputStream() throws IOException {
URLConnection con = url.openConnection();
useCachesIfNecessary(con);
try {
return con.getInputStream();
} catch (IOException ex) {
// Close the HTTP connection (if applicable).
if (con instanceof HttpURLConnection) {
((HttpURLConnection) con).disconnect();
}
throw ex;
}
}
内容来源于网络,如有侵权,请联系作者删除!