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!