Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Error Unexpected end of file from server with HTTP POST

843790Feb 9 2009 — edited Nov 23 2009
Hi everyone,

I'm coding a simple client to download some information from a local machine in my LAN.
I have to do this with an http post request.
When i try to parse the http response the program catch an exception, this one:

java.net.SocketException: Unexpected end of file from server
at sun.net.www.http.HttpClient.parseHTTPHeader(...)

the parameter is a JSON request, and of course the response is a JSON formatted.

i put the http request code:
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;




public class HttpDownloaderThread  extends Thread{
    
	private String url;
        private String param;
	private HttpDownloadListener listener;
	private HttpURLConnection connection=null;
	private InputStream is;
        private OutputStreamWriter wr;

	public HttpDownloaderThread(String _url,String param, HttpDownloadListener _listener){
		url = _url;
		listener = _listener;
                this.param=param;
                
	}

	public void run(){
		try{
                        
			connection=(HttpURLConnection)new URL(url).openConnection();
                        connection.setRequestMethod("POST");
                        connection.setReadTimeout(5000);
                        connection.setRequestProperty("Content-Type", "application/jsonrequest");
                        connection.setDoOutput(true);
                        wr = new OutputStreamWriter(connection.getOutputStream());
                        wr.write(param, 0, param.length());
                        wr.flush();
                        int responseCode=0;
			System.out.println();
                        try{
                         responseCode= connection.getResponseCode();
                        }catch(Exception e){
                            e.printStackTrace();
                        }
			if (responseCode == HttpURLConnection.HTTP_OK){
				is = connection.getInputStream();
                                 BufferedReader rd = new BufferedReader(new InputStreamReader(is));
                                String line;
                                while ((line = rd.readLine()) != null) {
                                    System.out.println(line);
                                }
				closeHttpConnection();
				listener.resourceDownloaded(url, null);
			                         }
                            else{
                            closeHttpConnection();
                            listener.downloadFailed(url, new Exception("Http error: " + Integer.toString(responseCode)));
                            }
		}catch(Exception e){
			e.printStackTrace();
			listener.downloadFailed(url, e);
		}finally{
		}
	}

	public void closeHttpConnection(){
		if (is != null){
			try{
				is.close();
                                wr.close();
			}catch (Exception e){
			}finally{
				is = null;
                                wr=null;
			}
		}
		if (connection != null){
			try{
				connection.disconnect();
			}catch (Exception e){
			}finally{
				connection = null;
			}
		}
	}
}
there's someone who know's why??

Thanks to everyone :)
Thomas.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 21 2009
Added on Feb 9 2009
5 comments
1,774 views