3

I have a totally normal class in Java for HTTP Connection to get a HTTP responce headers from a Server. I seems to be absolut okay, and it is actually works fine for the most of the web-sites that I've tested. But..

    import java.net.HttpURLConnection;
    import java.net.URL;
    public class Main2 {
        public static void main(String[] args) {
            HttpURLConnection.setFollowRedirects(false);
            HttpURLConnection con = null;
            try {
                URL url = new URL("http://sometestsite.blabla/");
                con = (HttpURLConnection) url.openConnection();
                con.setRequestMethod("GET");
                con.connect();

                System.out.println(con.getHeaderField("Server"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

BUT there are some sites, and by testing of them with this class, I get one time "java.net.ConnectException: Connection refused" Exception and secound time ... it works fine (by the same hostname) and I get an answer! And it goes so on and so on. One time an answer one time an exception.

Exception:

java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
    at sun.net.www.http.HttpClient.New(HttpClient.java:308)
    at sun.net.www.http.HttpClient.New(HttpClient.java:326)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1169)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1105)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:999)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:933)
    at Main2.main(Main2.java:14)

When I test the same hostname in Browser, I become always an answer. When I test the same hostname in curl (ubuntu), i get always an answer either (so in both cases no exceptions).

I'm really confused, please write if you have any ideas. Thanks a lot!

Oli
  • 94
  • 1
  • 10
  • 2
    It sounds like a problem with your networking, not with the application. – Stephen C Oct 18 '16 at 13:35
  • http://stackoverflow.com/questions/6876266/java-net-connectexception-connection-refused – Karthik Oct 18 '16 at 13:35
  • @Stephen C But why does it works (the same uri, on the same host with the same network settings) in curl and in browser? – Oli Oct 18 '16 at 13:37
  • @Karthik, I've read this post and coundn't fine there my case, because the problem is, that the same Java-Code makes by the first run an error and by the secound run I get an succesfull answer. – Oli Oct 18 '16 at 13:40
  • Are you sure that you are using the same settings in your application as in your browser? For example haveyou your browser connection settings? Does your browser uses a proxy, etc? – alainlompo Oct 18 '16 at 13:41
  • @alainlompo the browses uses system proxy settings. I didn't configured different settings for eclipse or for browser or for curl. – Oli Oct 18 '16 at 13:44
  • @Oli, if your browser uses proxy settings you should most probably have the same proxy configured in your code programmaticaly... try it and see if it solves your issue – alainlompo Oct 18 '16 at 13:52
  • @alainlompo I didn't configured a system proxy. So if i set i browser to use "no proxy", than it is the same result. So no proxy used. – Oli Oct 18 '16 at 14:12

2 Answers2

2

java.net.ConnectException occurs when you are working with client-server architecture and trying to make TCP connection from the client to the server. Now there are many reasons for it to happen. I understand in your case it is happening intermittently i.e sometimes it works and sometimes it doesn't.

There doesn't seem to be any problem with your client code, its either the server or the network. Network/Firewall is rarely intermittent. I would suggest you to do series of tests (ping/curl etc) on the server, that seems to be the problem here.

In addition you can also incorporate piece of this code in your client to better debug the issue. If everything fails, then talk to your networking team about the issue.

public class TestServer {
    public static void main(String[] args){
        try{
            InetAddress address = InetAddress.getByName("192.168.1.1");
            boolean reachable = address.isReachable(10000);
            System.out.println("Host reachable? " + reachable);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}
Himalay Majumdar
  • 3,883
  • 14
  • 65
  • 94
  • The result of this test is that the address is unreachable, but in console i become only the positive pings. So it seems to be like a server's firefall? – Oli Oct 18 '16 at 14:01
  • 1
    If you are in a corporate network, I would suggest you to engage your networking team, or give a try to WireShark packet sniffer. Your client is fine. – Himalay Majumdar Oct 18 '16 at 14:09
  • Wireshark gave me a hint, by connections with source port > 45000 the connection will be accepted, by smaller ports not. I don't know why, but by first connection Java chooses source port 36846 and by secound one 45852, so the secound one passes. – Oli Oct 18 '16 at 14:51
1

Now I can give an answer with information from my last comment:

Wireshark gave me a hint, by connections with source port > 45000 the connection will be accepted, by smaller ports not.By first connection Java choosed source port 36846 and by secound one 45852, so the secound one passes The curl also used 2 trys: one with a smaller port and one with a bigger (> 51000) so that passes too. The browser uses the first time port > 45000.

Result: seems to be a missconfigured firefall.

Oli
  • 94
  • 1
  • 10