java codes

This code is a simple, general purpose SOAP client in Java that uses XML Request File. This examples, we are sending a XML Request file with SOAP URL and getting back SOAP response as XML file.

The Java code, it is  opening up an HTTP connection, connecting through the proxy (you can comment it out if you are not using any proxy in your environment), sending the appropriate XML to invoke a remote method, and then reading the XML response returned by the server.

SOAP with Request XML

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* Call SOAP URL and send the Request XML and Get Response XML back */
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
 
public class SoapXML {
 
    public static void sendSoapRequest(String cisId) throws Exception {
        //use this if you need proxy to connect
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("YOUR PROXY", PORT NUMBER));
        String SOAPUrl = "WISDL URL HERE";
        String xmlFile2Send = ".\src\request.xml";
        String responseFileName = ".\src\response.xml";
 
        // Create the connection with http
        URL url = new URL(SOAPUrl);
        URLConnection connection = url.openConnection(proxy);
        HttpURLConnection httpConn = (HttpURLConnection) connection;
        FileInputStream fin = new FileInputStream(xmlFile2Send);
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
 
        copy(fin, bout);
        fin.close();
 
        byte[] b = bout.toByteArray();
        StringBuffer buf=new StringBuffer();
        String s=new String(b);
 
        //replacing a sample value in Request XML
        s=s.replaceAll("VALUE", value);
        b=s.getBytes();
 
        // Set the appropriate HTTP parameters.
        httpConn.setRequestProperty("Content-Length", String.valueOf(b.length));
        httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        httpConn.setRequestProperty("SOAPAction", "");
        httpConn.setRequestMethod("POST");
        httpConn.setDoOutput(true);
 
        // send the XML that was read in to b.
        OutputStream out = httpConn.getOutputStream();
        out.write(b);
        out.close();
 
        // Read the response.
        httpConn.connect();
        System.out.println("http connection status :"+ httpConn.getResponseMessage());
        InputStreamReader isr = new InputStreamReader(httpConn.getInputStream());
        BufferedReader in = new BufferedReader(isr);
 
        while ((inputLine = in.readLine()) != null)
            System.out.println(inputLine);
        FileOutputStream fos=new FileOutputStream(responseFileName);
        copy(httpConn.getInputStream(),fos);
        in.close();
    }
 
    public static void copy(InputStream in, OutputStream out)
            throws IOException {
 
        synchronized (in) {
            synchronized (out) {
                byte[] buffer = new byte[256];
                while (true) {
                    int bytesRead = in.read(buffer);
                    if (bytesRead == -1)
                        break;
                    out.write(buffer, 0, bytesRead);
                }
            }
        }
    }
}

In this example we are modifying a value of the default request XML, you can use this snippet to modify more varaibles in XML or you can comment it out. Please use SOAP UI client with the URL and request XML file to make sure it is working with your URL and XML file and getting back a XML response.

1 COMMENT

Leave a comment