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)
1
  • 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. Commented Feb 26, 2019 at 0:27

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for suggestion, but it didn't solve a problem.
If you need a https connection without verification any certificate you will be able to use something like stackoverflow.com/questions/13022717/… Otherwise you have to download a cert and add it to your connection.
Thanks, I've used the code with other sites with HTTPS connection , and it worked, without explicitly importing their certificate.
Thanks Alexey, i tried to remove proxy with obj.openConnection(Proxy.NO_PROXY); and its the same. Any suggestion?
Hello, do you get a same stack trace of this error?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.