TechnoBuzz

A Techno Blog, mainly about Java

Soap Client Homemade implementation

The Soap request:

<?xml version=”1.0″ encoding=”utf-8″?>

<soap:Envelope xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xmlns:xsd=”http://www.w3.org/2001/XMLSchema&#8221; xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/”&gt;
<soap:Body>
<GetInfoByZIP xmlns=”http://www.webserviceX.NET”&gt;
<USZip>27615</USZip>
</GetInfoByZIP>
</soap:Body>
</soap:Envelope>

The java program:

try {
// Set up the Web Service URL and SOAP Message file name
// http://www.webservicex.net/uszip.asmx
// src/exercise1/test.xml

String WebServiceUrl = args[0];
String SOAPFile = args[1];

//Create the connection where we’re going to send the file.
URL url = new URL(WebServiceUrl);
URLConnection connection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) connection;

//Get the SOAP document ready to send

File f1 = new File(SOAPFile);
String path = f1.getAbsolutePath();
FileInputStream FileIn = new FileInputStream(path);
ByteArrayOutputStream ByteOut = new ByteArrayOutputStream();
byte[] buffer = new byte[256];
while (true) {
int bytesRead = FileIn.read(buffer);
if (bytesRead == -1)
break;
ByteOut.write(buffer, 0, bytesRead);
}

byte[] b = ByteOut.toByteArray();
FileIn.close();

//Set the HTTP parameters.
httpConn.setRequestProperty(“Content-Length”, String.valueOf(b.length));
httpConn.setRequestProperty(“Content-Type”, “text/xml; charset=utf-8”);
httpConn.setRequestProperty(
“Accept”,
“application/soap+xml, application/dime, multipart/related, text/*”);
httpConn.setRequestProperty(“User-Agent”, “WSAD”);
httpConn.setRequestProperty(“Host”, “localhost”);
httpConn.setRequestProperty(“Cache-Control”, “no-chache”);
httpConn.setRequestProperty(“Pragma”, “no-chache”);
httpConn.setRequestProperty(“SOAPAction”, “http://www.webserviceX.NET/GetInfoByZIP&#8221;);
httpConn.setRequestMethod(“POST”);
httpConn.setDoOutput(true);
httpConn.setDoInput(true);

//Send XML File
OutputStream out = httpConn.getOutputStream();
out.write(b);
out.close();

//Get the response
InputStreamReader isr = new InputStreamReader(httpConn.getInputStream());
BufferedReader in = new BufferedReader(isr);

String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();

} catch (Exception e) {
System.out.println(“FaultString: ” + e.toString());
return;
} //end exception
}

Response:

<?xml version=”1.0″ encoding=”utf-8″?><soap:Envelope xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/&#8221; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xmlns:xsd=”http://www.w3.org/2001/XMLSchema”><soap:Body><GetInfoByZIPResponse xmlns=”http://www.webserviceX.NET”><GetInfoByZIPResult><NewDataSet xmlns=””><Table><CITY>Raleigh</CITY><STATE>NC</STATE><ZIP>27615</ZIP><AREA_CODE>919</AREA_CODE><TIME_ZONE>E</TIME_ZONE></Table></NewDataSet></GetInfoByZIPResult></GetInfoByZIPResponse></soap:Body></soap:Envelope>

Advertisements

October 21, 2012 - Posted by | Uncategorized

No comments yet.

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

%d bloggers like this: