free web site maker software download

A Simple SOAP Client With Java

BY ALI ULVI TALIPOGLU

While development or writing test cases we may need to send SOAP requests and check the response. Below is an example with Java using Apache's HttpClient library. 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;


public class SoapWebServiceClient {

	//This method sends a SOAP request and prints response, and parses and prints texts of all <balance> elements in the response 
	public void postSOAPXML(String exampleInputParameter) {
        String resp = null;
        try {

            String soapBody = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:aut=\"http://ulvi.aut.com/aut\">\n" +
                    "   <soapenv:Header/>\n" +
                    "   <soapenv:Body>\n" +
                    "      <aut:GetDataRequest>\n" +
                    "         <MiddlewareTransactionId>123456AUT</MiddlewareTransactionId>\n" +
                    "         <keyValue>" + exampleInputParameter + "</keyValue>\n" +
                    "         <returnRealtimeData>true</returnRealtimeData>\n" +
                    "      </aut:GetDataRequest>\n" +
                    "   </soapenv:Body>\n" +
                    "</soapenv:Envelope>";
            
            HttpClient httpclient = new DefaultHttpClient();
            // You can get below parameters from SoapUI's Raw request if you are using that tool
            StringEntity strEntity = new StringEntity(soapBody, "text/xml", "UTF-8");
            // URL of request
            HttpPost post = new HttpPost("http://10.1.1.3:8989/getData");
            post.setHeader("SOAPAction", "GetData");
            post.setEntity(strEntity);

            // Execute request
            HttpResponse response = httpclient.execute(post);
            HttpEntity respEntity = response.getEntity();

            if (respEntity != null) {
                resp = EntityUtils.toString(respEntity);
				
                //prints whole response
                System.out.println(resp);
               
                //Regular expression to parse texts of <balance> elements in the response assuming they have no child elements
                Matcher m = Pattern.compile("<balance>([^<]*)").matcher(resp); //groups all characters except < (tag closing character)
                while (m.find()) {
                  //prints texts of all balances in a loop
                  System.out.println(m.group(1));
                }
            } else {
                System.err.println("No Response");
            }

        } catch (Exception e) {
            System.err.println("WebService SOAP exception = " + e.toString());
        }
    }

	public static void main(String[] args) {
		SoapWebServiceClient soapWebServiceClientObject = new SoapWebServiceClient();
		soapWebServiceClientObject.postSOAPXML("057177711");
    }
}

FACEBOOK COMMENTS WILL BE SHOWN ONLY WHEN YOUR SITE IS ONLINE


free counter