The “java.net.SocketException: Unexpected end of file from server” exception is thrown in the HTTPClient class, as well as others. This article simply provides an example of how to generate it for testing purposes.
We start with a client class that will request a given URL from our custom python web server…
import java.net.*; import java.util.*; public class javaUrl { public static void main (String args[]) throws Exception { URL url = new URL("http://localhost:9000/t.html"); URLConnection conn = url.openConnection(); Thread.sleep(2000); System.out.println(url.getContent().toString()); Map> map = conn.getHeaderFields(); for (Map.Entry > entry : map.entrySet()) { System.out.println("Key : " + entry.getKey() + " ,Value : " + entry.getValue()); } } }
…which is shown below. Notice that we accept the connection, but simply return from the method, returning no data associated with the URL we requested. The specific exception is thrown because we did not return a complete HTTP response, which includes a content-type and a response code…
import string,cgi,time from os import curdir, sep from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer #import pri class MyHandler(BaseHTTPRequestHandler): def do_GET(self): try: f = open("t.html") #self.path has /test.html return self.send_response(200) self.send_header('Content-type', 'text/html') self.send_header('Content-length',20000) self.end_headers() sr = f.read() print sr self.wfile.write(sr) f.close() return except IOError: self.send_error(404,'File Not Found: %s' % self.path) try: server = HTTPServer(('', 9000), MyHandler) print 'started httpserver...' server.serve_forever() except KeyboardInterrupt: print '^C received, shutting down server' server.socket.close()
The example of running this is shown below…
[adm-showard@cmhlcarchapp01 ~]$ java javaUrl Exception in thread "main" java.net.SocketException: Unexpected end of file from server at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:778) at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:633) at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:775) at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:633) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1325) at java.net.URLConnection.getContent(URLConnection.java:748) at java.net.URL.getContent(URL.java:1055) at javaUrl.main(javaUrl.java:8) [adm-showard@cmhlcarchapp01 ~]$
It should be noted that this differs from “java.net.SocketException: Connection reset” exception, as that is thrown when the client receives an actual RST packet from the target server.
Below is the output from a wireshark session for a “java.net.SocketException: Unexpected end of file from server” exception…
…and for a “java.net.SocketException: Connection reset” exception…
Sources/reason for the exceptions below are provided for clarity:
* return before a response code or headers, we get “java.net.SocketException: Unexpected end of file from server”.
* return only a response code, we get “java.net.UnknownServiceException: no content-type”
* return a content-type, but no response code, we get “java.io.IOException: Invalid Http response”