Ron
Ron

Reputation: 29

How to send soap xml request and read response

I created code with java which send soap xml request and and should read response but i received these errors Internal Server Error:

java.io.IOException: Server returned HTTP response code: 500 for URL: https://test.api.system.simplyclub.co.il/TerminalService.asmx?op=UserAccountLogin.

what could be the issue?

 package com.test.com;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Send_Xml {
    
 public static void main(String[] args) {
    

     try {
         String url = "https://test.api.system.simplyclub.co.il/TerminalService.asmx?op=UserAccountLogin";
         URL obj = new URL(url);
         HttpURLConnection con = (HttpURLConnection) obj.openConnection();
         con.setRequestMethod("POST");
         con.setRequestProperty("Content-Type","text/xml; charset=utf-8");
         String xml = 
         "<?xml version=\\\"1.0\\\" encoding=\\\"utf-8\\\"?>"+
         "<soap:Envelope xmlns:xsi=\\\"http://www.w3.org/2001/XMLSchema-instance\\\" xmlns:xsd=\\\"http://www.w3.org/2001/XMLSchema\\\" xmlns:soap=\\\"http://schemas.xmlsoap.org/soap/envelope/\\\">"+
         "<soap:Body>"+
         "<UserAccountLogin xmlns=\\\"http://tempuri.org/\\\">"+
         "<account>"+
         "<VendorApiKey>string</VendorApiKey>"+
         "<User>string</User>"+
         "<Password>string</Password>"+
         "</account>"+
         "<additionalInfo>"+
         "<FieldData>"+
         "<FieldId>string</FieldId>"+
         "<FieldValue>string</FieldValue>"+
         "</FieldData>"+
         "<FieldData>"+
         "<FieldId>string</FieldId>"+
         "<FieldValue>string</FieldValue>"+
         "</FieldData>"+
         "</additionalInfo>"+
         "</UserAccountLogin>"+
         "</soap:Body>"+
         "</soap:Envelope>";
         con.setDoOutput(true);
         DataOutputStream wr = new DataOutputStream(con.getOutputStream());
         wr.writeBytes(xml);
         wr.flush();
         wr.close();
         String responseStatus = con.getResponseMessage();
         System.out.println(responseStatus);
         BufferedReader in = new BufferedReader(new InputStreamReader(
         con.getInputStream()));
         String inputLine;
         StringBuffer response = new StringBuffer();
         while ((inputLine = in.readLine()) != null) {
         response.append(inputLine);
         }
         in.close();
         System.out.println("response:" + response.toString());
         } catch (Exception e) {
         System.out.println(e);
         }
         }
        }

Upvotes: -1

Views: 226

Answers (1)

Stephen C
Stephen C

Reputation: 719376

What is the meaning of Internal Server Error ...

It is an HTTP response status code:

"The HyperText Transfer Protocol (HTTP) 500 Internal Server Error server error response code indicates that the server encountered an unexpected condition that prevented it from fulfilling the request."

Source

in JAVA ...

It is not a Java error. It is a response code returned by the server that you talking to. And that server is not necessarily implemented in Java.

and how to fix that?

This is the tricky part. There are three approaches you could take:

  1. An HTTP response typically has a body. For an error response (4xx or 5xx) the response body may contain information that will help you to figure out what the problem is.

    Try reading con.getErrorStream() instead of con.getOutputStream().

  2. If you own / manage the server, you should check the server log files. They may contain error messages, stack traces and so on that will help you figure out what the problem is.

  3. If you don't have access to the server logs, you could try contacting the site managers.

Once you understand the cause you may be able to either fix the server-side bug, or work out how to avoid it. For example, maybe you triggered the bug by sending a malformed request. But we can't tell you what the problem is or how to fix it without seeing the diagnostics.

Having said that ... is there some reason why you are double-escaping the "s in this line (for example):

  "<?xml version=\\\"1.0\\\" encoding=\\\"utf-8\\\"?>" +

Upvotes: 1

Related Questions