T
T
Timur Evgazhukov2017-04-11 15:24:48
Java
Timur Evgazhukov, 2017-04-11 15:24:48

What can cause networking to hang in a java application?

Wrote a site parsing that works through various proxies. Everything works great, but at some point the execution of the program stops and hangs like this until you reboot. No errors or messages are given. Determined the place in the code where everything is connected. Below I indicated the problematic piece of code and the line where execution stops at some point. Am I incorrectly clearing resources and closing connections? Or maybe there are other problems? Where can I dig for a solution?
HttpsURLConnection conn = null;
BufferedReader in = null;
try {
System.setProperty("https.protocols", "TLSv1.1");
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyMapItem.getLink(), proxyMapItem.getPort()));
conn = (HttpsURLConnection) new URL(link).openConnection(proxy);
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; H010818)");
// here after a while everything freezes !!!!!!
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringstrLine;
StringBuilder finalHTML = new StringBuilder();
while ((strLine = in.readLine()) != null) {
finalHTML.append(strLine);
}
doc = Jsoup.parse(finalHTML.toString());
connectFlag=true;
} catch (HttpStatusException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
System.out.println(">>>>> Let's try to download again from the link: " + link);
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println(">>>>> Let's try to download again from the link: " + link);
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (conn != null) {
conn.disconnect();
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Alexandrov, 2017-04-11
@evgajukov

1) There are tags for the code.
2) It is better to use the try-with-resources construct
3) I'm not completely sure because it is desirable to understand exactly how the given code is used, but it is highly likely that the problem is with the resources, something is closed or not opened.
3.1) No less likely is the problem in proxies, perhaps a dead proxy and the timeout connection does not stop and stupidly starts waiting for it to come.
3.2) Check responseCode . First you do conn.connect(). then conn.getResponseCode(). See if the response code is correct.
3.3) Be sure to manually set the timeout conn.setConnectTimeout (milliseconds) and only after all the manipulations, incl. getting responseCode;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question