6

So this is a beginner's question.

When executing the sample code from the working with urls chapter it throws:

Exception in thread "main" java.net.SocketException: Connection reset at 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 there are similar threads regarding that topic, but i could not find an answer that suits me.

What I've tried so far:

  • I have set the proxy host as suggested here. Command was: java -Dhttp.proxyHost=dslb-088-071-100-199.pools.arcor-ip.net, I also tried it with inserting 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) method to have a similar result.

Whatever I try, I always get the same error: There will happen nothing for 30 seconds or so and then it throws the mentioned SocketException.

I simply dont know how to continue in solving this problem. Helpful would be to get information about what happens in background during the 30seconds before connection reset.

So what could actually cause this Exception?

The smallest hint could help! Thank you!

achingfingers
  • 1,827
  • 5
  • 24
  • 48
  • 1
    Can you give us the full code with the proxy host set... Your code by the way executes successfully on my machine. – Menelaos Jun 05 '13 at 14:29
  • It should work..... Do you have access to to this url from your browser. – Makky Jun 05 '13 at 14:30
  • This can be helpful: http://stackoverflow.com/questions/62929/java-net-socketexception-connection-reset – Adam Siemion Jun 05 '13 at 14:30
  • It gives me an `UnknownHostException`, as well as on IDEONE. – Djon Jun 05 '13 at 14:30
  • @meewoK: This is the full code. Proxy host was set via command line. – achingfingers Jun 05 '13 at 14:31
  • @Makky: Sure I do have. – achingfingers Jun 05 '13 at 14:32
  • @achingfingers So your problem could very well be a connection issue due to not being able to reach the website through the proxy. Can you give us the command you used via commandline. – Menelaos Jun 05 '13 at 14:33
  • Maybe your proxy need any type of authentication? – mauretto Jun 05 '13 at 14:35
  • @achingfingers Your source works fine for me! – My-Name-Is Jun 05 '13 at 14:35
  • @achingfingers I'm 100% positive it sounds like a proxy issue. Try testing your proxy via telnet / putty and other browsers. Also what type of authentication/port/protocol. – Menelaos Jun 05 '13 at 14:37
  • @meewoK the command was: java -Dhttp.proxyHost=dslb-088-071-100-199.pools.arcor-ip.net – achingfingers Jun 05 '13 at 14:38
  • @meewoK: What do you mean by "it sounds like a proxy"? Sorry I am a complete noob to the whole url connection thing. – achingfingers Jun 05 '13 at 14:40
  • @achingfingers I mean that since the code executes fine without using the proxy, it means the code doesn't have the problem. It could very well be the proxy your trying to use is not online or available. Why don't you try running without the proxy `java app` – Menelaos Jun 05 '13 at 14:41
  • @meewoK: How can I disable the proxy then? I've already set the proxy settings to the one my default browser uses ... – achingfingers Jun 05 '13 at 14:44
  • Just run your application without the `-Dhttp.proxyHost=dslb-088-071-100-199.pools.arcor-ip.net` . Does all your traffic go through it? Do you need it because your admin says so or something like that? – Menelaos Jun 05 '13 at 14:45
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/31264/discussion-between-achingfingers-and-meewok) – achingfingers Jun 05 '13 at 14:46

2 Answers2

2

Your code is working fine for JVM's that can connect to the internet.

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

  • An intermediate firewall is blocking the JVM from making the connection (or another similar network issue).
  • An operating system firewall, or antivirus that is causing the problems as well.

My suggestion is to try:

  • Same app on different computer within same network (to see if it is PC specific).
  • Same app on different network.
Community
  • 1
  • 1
Menelaos
  • 23,508
  • 18
  • 90
  • 155
  • 1
    `} catch (Exception x) { System.out.println("No Connection"); }` throws away the exception message and is way worse than just declaring main as `throws Exception` – artbristol Jun 05 '13 at 15:50
  • Yes, except this was just a quick test I picked up somewhere to see if the OP was getting 200 or not. Please see the whole discussion within the chat. – Menelaos Jun 05 '13 at 15:52
  • 1
    So in my case it has been the antivirus software that was causing the problem. After disabling it's browser protection function everything worked well ... Thank you – achingfingers Jun 07 '13 at 09:07
-1

Try Apache HTTPClient. I hope all the imports are included as this code is not tested as it is... Also your 30s is the connection timeout of your client.

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();
    }
}
Balint Bako
  • 2,500
  • 1
  • 14
  • 13