简体   繁体   中英

Exception: java.net.SocketException: Unexpected end of file from server

I have the following code and I keep getting an error

  public static HttpsURLConnection getRestConnectionWithProxy(String urlString) throws MalformedURLException,
                                                                                         IOException {
       
        URL url = new URL(null, urlString, new sun.net.www.protocol.https.Handler());
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(getProxy());
       
        return conn;
    }

private static Proxy getProxy() {
    if (proxy == null) {
        Map<String, String> connectionParams = null;
        
        String proxyIP = "proxyIP";
        int proxyPort = 80;
        proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyIP, proxyPort));


    }
    return proxy;
}

The error that I get is the following

Exception: java.net.SocketException: Unexpected end of file from server
        at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:851)
        at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:678)
        at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:848)
        at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:678)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1587)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
        at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
        at java.net.HttpURLConnection.getResponseMessage(HttpURLConnection.java:546)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseMessage(HttpsURLConnectionImpl.java:391)

Do you know maybe what could be the cause of this error?

As you can see in stack-trace, there is possibility that server -- with which you are communicating -- suddenly closed connection after it got established. Possibly, server is unable to serve your requests at moment or there is issue in firewall / router which is dropping network packets.

There can be issues like Authentication header missing etc but can't confirm as shared information is limited.

You might need to start looking at

  1. Credential info
  2. Check router logs if there is any; to see if router is having any issue.
  3. Try to see if your encoding (eg UTF-8 ) is proper along with content-type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM