Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webservice call returns error 500

I have started a small project in Java.
I have to create a client which will send xml to a url as a HTTP POST request.
I try it using java.net.* package (Following is the piece of code) but I am getting error as follows:

java.io.IOException: Server returned HTTP response code: 500 for URL: "target url"
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
    at newExample.main(newExample.java:36)

My code is as follows:

try {
        URL url = new URL("target url");

        URLConnection connection = url.openConnection();

        if( connection instanceof HttpURLConnection )
            ((HttpURLConnection)connection).setRequestMethod("POST");

        connection.setRequestProperty("Content-Length", Integer.toString(requestXml.length()) );
        connection.setRequestProperty("Content-Type","text/xml; charset:ISO-8859-1;");
        connection.setDoOutput(true);
        connection.connect();           

        // Create a writer to the url
        PrintWriter writer = new PrintWriter(new
        OutputStreamWriter(connection.getOutputStream()));

        // Get a reader from the url
        BufferedReader reader = new BufferedReader(new
        InputStreamReader(connection.getInputStream()));

        writer.println();
        writer.println(requestXml);
        writer.println();
        writer.flush();

        String line = reader.readLine();
            while( line != null ) {
                    System.out.println( line );
                    line = reader.readLine();
            }


    } catch (MalformedURLException e) {
                    e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Please help with suitable examples or any other ways of doing this.

Point errors/mistakes in above code or other possibilities.

My Web Service is in spring framework

xml to send is in the string format: requestXml

like image 704
sushil Avatar asked Mar 01 '11 08:03

sushil


2 Answers

The problem lies in below code

// Get a reader from the url
BufferedReader reader = new BufferedReader(new
InputStreamReader(connection.getInputStream()));

As the service might not always return you the proper response... as you are calling a service through http, it can be possible that the server itself is not available or the service is not available. So you should always check for the response code before reading response from streams, based on the response code you've to decide whether to read it from inputStream for success response or from errorStream for failure or exception condition.

BufferedReader reader = null;

if(connection.getResponseCode() == 200)
{
reader = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
}
else
{
reader = new BufferedReader(new
InputStreamReader(connection.getErrorStream()));
}

This would resolve the problem

like image 107
LuckyNandish Avatar answered Sep 23 '22 08:09

LuckyNandish


The problem is inside your server code or the server configuration:

10.5.1 500 Internal Server Error

The server encountered an unexpected condition which prevented it from fulfilling the request.

(w3c.org/Protocols)

If the server is under your control (should be, if I look at the URL [before the edit]), then have a look at the server logs.

like image 40
Andreas Dolk Avatar answered Sep 21 '22 08:09

Andreas Dolk