Java tutorial sample code throws: java.net.SocketException: Connection reset - what causes it?

So this is a beginner question.

When executing the example code from working with URLs , it throws:

Exception in thread "main" java.net.SocketException: Connection reset on java.net.SocketInputStream.read (SocketInputStream.java:189) ...

Origin is the openStream () method.

Here is the code:

import java.net.*; import java.io.*; public class URLReader { public static void main(String[] args) throws Exception { URL oracle = new URL("http://www.oracle.com/"); BufferedReader in = new BufferedReader( new InputStreamReader(oracle.openStream())); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); } in.close(); } } 

I know that there are similar topics for this topic, but I could not find an answer that suits me.

What I have tried so far :

  • I set the proxy host as suggested here . The command was: java -Dhttp.proxyHost = dslb-088-071-100-199.pools.arcor-ip.net, I also tried it with the insert System.setProperty ("http.proxyHost", "dslb-088-071- 100-199.pools.arcor-ip.net "); in the first line of the URLReader class.
  • I tried JSoup html parser and
  • org.apache.commons.io.FileUtils.copyURLToFile (URL, File) to have a similar result.

No matter what I try, I always get the same error: nothing will happen for 30 seconds or so, and then it throws the specified SocketException.

I just don’t know how to continue solving this problem. It would be useful to get information about what happens in the background for 30 seconds before the reset connection.

So what can actually cause this exception?

The smallest hint can help! Thanks!

+6
source share
2 answers

Your code works fine for a JVM that can connect to the Internet.

Based on the original question and discussion: http://chat.stackoverflow.com/rooms/31264/discussion-between-achingfingers-and-meewok it seems that either:

  • An intermediate firewall blocks the JVM from connecting (or another similar network problem).
  • an operating system firewall , or an antivirus that also causes problems.

My suggestion is to try:

  • The same application on another computer on the same network (to find out if it is specific to a PC).
  • Such an application on another network.
+2
source

Try Apache HTTPClient . I hope that all imports are included, since this code is not checked, since it ... Also, your 30s are a timeout for your client's connection.

 import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.ProxySelector; import java.net.SocketAddress; import java.net.URI; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.conn.params.ConnRoutePNames; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.CoreConnectionPNames; public class URLReader { public static void main(String[] args) throws Exception { HttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter( CoreConnectionPNames.CONNECTION_TIMEOUT, timeOut); httpclient.getParams().setParameter( CoreConnectionPNames.SO_TIMEOUT, 2 * timeOut); httpclient.getParams().setParameter( CoreConnectionPNames.STALE_CONNECTION_CHECK, false); httpclient.getParams().setParameter( CoreConnectionPNames.TCP_NODELAY, true); HttpHost proxy = new HttpHost(%proxyhost%, %proxyport%); HttpGet httpget = new HttpGet("http://www.oracle.com"); HttpResponse resp = httpclient.execute(httpget); respCode = resp.getStatusLine().getStatusCode(); BufferedReader br = new BufferedReader(new InputStreamReader(resp .getEntity().getContent())); String line = null; while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); } } 
0
source

Source: https://habr.com/ru/post/946597/


All Articles