Danijel Sudimac
Danijel Sudimac

Reputation: 1

java.net.SocketException: Connection reset, using HttpURLConnection on page GET

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)

Upvotes: 0

Views: 5822

Answers (1)

JAlexey
JAlexey

Reputation: 114

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.

Upvotes: 1

Related Questions