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 | Leave a comment

Soap Web Services Begin To End Part 3

Using the WSDL in Part 2, right click on it in the sidebar:

WebService -> Generate Java Bean Skeleton

Again move the notch up to  Test Service  and click Next until it completes

Then, you will see in src folder all the generated files.

This completes the Producer  side of the web service.

Now, to generate a client side, you generate a new dynamic project (i.e. Call it MyClientProject).  After the dynamic project is complete, you can go into the Producer project, and get the wsdl and place it in the new Client project. Right click on the WSDL, click WebService -> Generate Client.

 

http://localhost:8080/MyClientProject/sampleCostServiceProxy/TestClient.jsp

 

October 21, 2012 Posted by | Uncategorized | Leave a comment

Soap Web Services Begin To End Part 2

Create a Dynamic Web project in eclipse. In my case I called it NumbersService.

Create a new class :
learn.webservices.numbers.ElectricCompanyService :

public class ElectricCompanyService {

public int multiply(int number1, int number2)
{
return number1 * number2;
}

public int add(int number1, int number2)
{
return number1 – number2;
}

}

Right click in the siderbar on ElectricCompanyService

WebService-> Create Web Service

On the screen that comes up, move the selector up one notch where it shows Test Service

In Web Content you can find a folder named WSDL with the WSDL file

<?xml version=”1.0″ encoding=”UTF-8″?>
<wsdl:definitions targetNamespace=”http://numbers.webservices.learn&#8221; xmlns:apachesoap=”http://xml.apache.org/xml-soap&#8221; xmlns:impl=”http://numbers.webservices.learn&#8221; xmlns:intf=”http://numbers.webservices.learn&#8221; xmlns:wsdl=”http://schemas.xmlsoap.org/wsdl/&#8221; xmlns:wsdlsoap=”http://schemas.xmlsoap.org/wsdl/soap/&#8221; xmlns:xsd=”http://www.w3.org/2001/XMLSchema”&gt;
<!–WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)–>
<wsdl:types>
<schema elementFormDefault=”qualified” targetNamespace=”http://numbers.webservices.learn&#8221; xmlns=”http://www.w3.org/2001/XMLSchema”&gt;
<element name=”add”>
<complexType>
<sequence>
<element name=”number1″ type=”xsd:int”/>
<element name=”number2″ type=”xsd:int”/>
</sequence>
</complexType>
</element>
<element name=”addResponse”>
<complexType>
<sequence>
<element name=”addReturn” type=”xsd:int”/>
</sequence>
</complexType>
</element>
<element name=”multiply”>
<complexType>
<sequence>
<element name=”number1″ type=”xsd:int”/>
<element name=”number2″ type=”xsd:int”/>
</sequence>
</complexType>
</element>
<element name=”multiplyResponse”>
<complexType>
<sequence>
<element name=”multiplyReturn” type=”xsd:int”/>
</sequence>
</complexType>
</element>
</schema>
</wsdl:types>

<wsdl:message name=”multiplyResponse”>

<wsdl:part element=”impl:multiplyResponse” name=”parameters”>

</wsdl:part>

</wsdl:message>

<wsdl:message name=”multiplyRequest”>

<wsdl:part element=”impl:multiply” name=”parameters”>

</wsdl:part>

</wsdl:message>

<wsdl:message name=”addResponse”>

<wsdl:part element=”impl:addResponse” name=”parameters”>

</wsdl:part>

</wsdl:message>

<wsdl:message name=”addRequest”>

<wsdl:part element=”impl:add” name=”parameters”>

</wsdl:part>

</wsdl:message>

<wsdl:portType name=”ElectricCompanyService”>

<wsdl:operation name=”add”>

<wsdl:input message=”impl:addRequest” name=”addRequest”>

</wsdl:input>

<wsdl:output message=”impl:addResponse” name=”addResponse”>

</wsdl:output>

</wsdl:operation>

<wsdl:operation name=”multiply”>

<wsdl:input message=”impl:multiplyRequest” name=”multiplyRequest”>

</wsdl:input>

<wsdl:output message=”impl:multiplyResponse” name=”multiplyResponse”>

</wsdl:output>

</wsdl:operation>

</wsdl:portType>

<wsdl:binding name=”ElectricCompanyServiceSoapBinding” type=”impl:ElectricCompanyService”>

<wsdlsoap:binding style=”document” transport=”http://schemas.xmlsoap.org/soap/http”/&gt;

<wsdl:operation name=”add”>

<wsdlsoap:operation soapAction=””/>

<wsdl:input name=”addRequest”>

<wsdlsoap:body use=”literal”/>

</wsdl:input>

<wsdl:output name=”addResponse”>

<wsdlsoap:body use=”literal”/>

</wsdl:output>

</wsdl:operation>

<wsdl:operation name=”multiply”>

<wsdlsoap:operation soapAction=””/>

<wsdl:input name=”multiplyRequest”>

<wsdlsoap:body use=”literal”/>

</wsdl:input>

<wsdl:output name=”multiplyResponse”>

<wsdlsoap:body use=”literal”/>

</wsdl:output>

</wsdl:operation>

</wsdl:binding>

<wsdl:service name=”ElectricCompanyServiceService”>

<wsdl:port binding=”impl:ElectricCompanyServiceSoapBinding” name=”ElectricCompanyService”>

<wsdlsoap:address location=”http://localhost:8080/NumbersService/services/ElectricCompanyService”/&gt;

</wsdl:port>

</wsdl:service>

</wsdl:definitions>

:

October 21, 2012 Posted by | Uncategorized | Leave a comment

Soap Web Services Begin To End Part I

Step 1 : Open Web Perspective -> Web Services Explorer




Launch Web services explorer:


Select WSDL page (next to star on right):


Enter the SDL URL : http://www.webservicex.net/stockquote.asmx?WSDL

Choose Soap enter stock quote and Go!

<soapenv:Envelope xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/” xmlns:q0=”http://www.webserviceX.NET/” xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance“>

<soapenv:Body>

<q0:GetQuote>

  <q0:symbol>IBM</q0:symbol>

  </q0:GetQuote>

  </soapenv:Body>

  </soapenv:Envelope>

<soap:Envelope xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/” xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance“>

<soap:Body>

<GetQuoteResponse xmlns=”http://www.webserviceX.NET/“>

  <GetQuoteResult><StockQuotes><Stock><Symbol>IBM</Symbol><Last>193.36</Last><Date>10/19/2012</Date><Time>4:03pm</Time><Change>-1.60</Change><Open>195.12</Open><High>196.08</High><Low>193.18</Low><Volume>6621563</Volume><MktCap>219.9B</MktCap><PreviousClose>194.96</PreviousClose><PercentageChange>-0.82%</PercentageChange><AnnRange>177.06 – 211.79</AnnRange><Earns>13.913</Earns><P-E>14.01</P-E><Name>International Bus</Name></Stock></StockQuotes></GetQuoteResult>

  </GetQuoteResponse>

  </soap:Body>

  </soap:Envelope>


Here is a WSDL for Mortgage  payments calculation:

http://www.webservicex.net/mortgage.asmx?WSDL

October 21, 2012 Posted by | Uncategorized | Leave a comment