Consuming web service using Apache HTTPClient and SOAP message

This code is used for quick testing purpose.

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;

/**
* This program demonstrates consuming web service using Apache HTTPClient and SOAP message.
* Reference: http://www.webservicex.net/stockquote.asmx?op=GetQuote
* ClassPath: Keep these two files in class path: commons-httpclient-3.0.1.jar, commons-codec-1.4.jar
* @author Bhavani P Polimetla
* @since April-27-2011
*/
public class WSClient {

public static void main(String args[]) {

HttpClient httpClient = new HttpClient();
httpClient.getParams()
.setParameter("http.useragent", "Web Service Test Client");

BufferedReader br = null;
String data = "<?xml version="1.0" encoding="utf-8"?> "
+ "<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">   "
+ "<soap12:Body>     "
+ "<GetQuote xmlns="http://www.webserviceX.NET/">       "
+ "<symbol>INFY</symbol>     " + "</GetQuote>   "
+ "</soap12:Body> </soap12:Envelope>";
PostMethod methodPost = new PostMethod(
"http://www.webservicex.net/stockquote.asmx?op=GetQuote");

methodPost.setRequestBody(data);
methodPost.setRequestHeader("Content-Type", "text/xml");

try {
int returnCode = httpClient.executeMethod(methodPost);

if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
System.out
.println("The Post method is not implemented by this URI");
methodPost.getResponseBodyAsString();
} else {
br = new BufferedReader(new InputStreamReader(methodPost
.getResponseBodyAsStream()));
String readLine;
while (((readLine = br.readLine()) != null)) {
System.out.println(readLine);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
methodPost.releaseConnection();
if (br != null)
try {
br.close();
} catch (Exception fe) {
fe.printStackTrace();
}
}

}
}

Output:

<!--?xml version="1.0" encoding="utf-8"?--><?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetQuoteResponse xmlns="http://www.webserviceX.NET/"><GetQuoteResult><StockQuotes><Stock><Symbol>INFY</Symbol><Last>65.99</Last><Date>4/27/2011</Date><Time>3:46pm</Time><Change>-0.27</Change><Open>66.25</Open><High>66.30</High><Low>65.31</Low><Volume>1689441</Volume><MktCap>37.701B</MktCap><PreviousClose>66.26</PreviousClose><PercentageChange>-0.41%</PercentageChange><AnnRange>53.28 - 77.92</AnnRange><Earns>2.62</Earns><P-E>25.29</P-E><Name>Infosys Technolog</Name></Stock></StockQuotes></GetQuoteResult></GetQuoteResponse></soap:Body></soap:Envelope>
Advertisements

5 thoughts on “Consuming web service using Apache HTTPClient and SOAP message

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s