Webservice 500 call return error

I started a small Java project.
I have to create a client that will send xml to the url as an HTTP POST request.
I am using the java.net.* Package (the following code snippet), but I get the 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 to do this.

Point errors / errors in the above code or other features.

My web service is in spring infrastructure

The xml to send is in the format of the string: requestXml

+8
java webservice-client
source share
5 answers

The problem is the following code

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

Since the service may not always return the correct answer ... because you are calling the service via http, it is possible that the server itself is unavailable or the service is unavailable. Therefore, you should always check the response code before reading the response from the streams, based on the response code, which you must decide whether to read it from inputStream to respond to success or from errorStream to refuse or exception.

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

This will solve the problem.

+21
source share

The problem is inside your server code or server configuration:

10.5.1 500 Internal server error

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

( w3c.org/Protocols )

If the server is under your control (it should be if I look at the URL [before editing]), look at the server logs.

+3
source share

Well, you have to close your threads and connections. An automatic resource base from Java 7 or http://projectlombok.org/ can help. However, this is probably not the main problem.

The main problem is that the server side is not working. An HTTP code of 500 indicates a server-side error. I can’t tell you the reason, because I don’t know the part of the server. Perhaps you should look at the server log.

+1
source share

I think your problem is that you open the input stream before you write and close the output stream. Of course, the Sun Tutorial does just that.

If you quickly open the input stream, it is possible that the output stream will be closed automatically, as a result of which the server will see an empty POST request. This may be enough to make him confused and send a 500 response.

Even if this is not what causes 500 errors, it is a good idea to do things in the order shown in the tutorial. To begin with, if you accidentally read the answer before you finish writing the request, you will most likely (at least temporarily) block the connection. (Actually, it looks like your code is doing this because you are not closing the record before reading from the reader.)

A separate problem is that your code does not close the connection under any circumstances and, therefore, network connections may leak. If he does this repeatedly, it will most likely lead to more IOExceptions.

+1
source share

If you call an external web service and pass JSON in a REST call, check the data type of the passed values.

Example:

 { "originalReference":"8535064088443985", "modificationAmount": { "amount":"16.0", "currency":"AUD" }, "reference":"20170928113425183949", "merchantAccount":"MOM1" } 

In this example, the sum value was sent as a string, and the web service call failed with a response to the HTTP server response: 500. But when the sum was sent: 16.0, that is, Integer was accepted, the call passed. Although you referenced the API documentation when invoking such external APIs, you might have missed out on small details like this.

0
source share

All Articles