0

Programmatically getting page with Java (HttpURLConnection) gives me error java.net.SocketException: Connection reset. Works with Python and with Postman.

Tried with other pages like gmail login page, ebanking login page etc. and it works, but only for one site (the one I need it to work) doesn't. Succeded with Python script, Postman also works, but Java doesn't

String url="https://********************/Login";
URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
        con.setRequestProperty("Cache-Control", "no-cache");
        con.setRequestMethod("GET");

        int responseCode = con.getResponseCode();

Expected to not throw exception. Actual result is:

java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:210)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
    at sun.security.ssl.InputRecord.read(InputRecord.java:503)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:975)
    at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:933)
    at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:735)
...

Python script which works:

import urllib.request  as urllib2 
content = urllib2.urlopen('https://******************/Login').read()
print(content)
  • The server does not like the HttpClient for some reason. Maybe it thinks something is malformed or incorrect SSL cipher suites. Connection reset just means that the connection was terminated by the other party abnormally. You should do a pcap and look at the server logs. – Johnny V Feb 26 '19 at 00:27

1 Answers1

1

Try to HttpsURLConnection:

String url = "https://********************/Login";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

Code in sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:735)

enter image description here

maybe a proxy is reason of the problem.

JAlexey
  • 114
  • 4