-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HttpConnection#prepareByteData() : should close bodyStream to reuse socket connection #1232
Labels
Milestone
Comments
a test case import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class URLConnectionDemo {
public static void main(String[] args) throws IOException {
fetch_url("http://localhost/");
fetch_url("http://localhost/index.html");
}
public static void fetch_url(String link) throws IOException {
URL url = new URL(link);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream in = conn.getInputStream();
Files.copy(in, Paths.get("temp"), StandardCopyOption.REPLACE_EXISTING);
in.close();
conn.disconnect();
}
} File Server: My Java Server https://github.com/h4675/web run: java wjs.sample.Debug |
a better way: close private void safeClose() {
if (bodyStream != null) {
try {
bodyStream.close();
} catch (IOException e) {
// no-op
} finally {
bodyStream = null;
}
}
if (conn != null) {
conn.disconnect();
conn = null;
}
} |
Thanks! |
This was referenced Feb 9, 2020
This was referenced Feb 10, 2020
This was referenced Feb 18, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It is significant when crawling maven repo index,
Will reuse the socket connection
Will close and open a new socket connection
If the InputStream closed & the host not changed,
java.net.HttpURLConnection
will reuse the socket, though invoked disconnect().I do the following change:
The text was updated successfully, but these errors were encountered: