Server Implementation and WSDL

Communicating with Web Services in a Nonstandard Way

If you do not have a Web Services client for your platform and you still wish to communicate with a Web Service, this is possible to do in a nonstandard way. This means that you ignore many of the standards mentioned in this book, and use both HTTP Post and Get methods to send information to your application. This section addresses this topic using a Java example and some HTML forms.

Making a Request to an Axis Web Service with HTML

Both Axis and .NET Web Services respond to Post and Get requests. Using an HTML form with Axis will demonstrate this. Consider the following HTML.

    <HTML>     <HEAD>        <TITLE>Test Web Service</TITLE>     </HEAD>     <FORM METHOD="GET"      ACTION="http://localhost:8080/axis/MoneyExchange.jws">      Method Name:      <input type="text"             name="method"             value="returnCurrencyName">      <BR>      Country Name:      <input type="text"             name="countryName"             value="Argentina">      <input type="submit">     </FORM>     </HTML>

Figure 13.5 displays the HTML form within Internet Explorer.

click to expand
Figure 13.5: The HTML form that communicates with the MoneyExchange Axis Web Service.

The HTML makes a Get request to the MoneyExchange Web Service described in Chapter 11. The default values of the HTML text inputs call the returnCurrencyName method with a default value of the countryName being Argentina. When you load the form into Internet Explorer and submit it, you’ll get the following response for the default values.

    <p>Got response message</p>     <?xml version="1.0" encoding="UTF-8"?>     <soapenv:Envelope      xmlns:soapenv=     ""      xmlns:xsd=     ""      xmlns:xsi=      "">      <soapenv:Body>      <returnCurrencyNameResponse soapenv:encodingStyle=      "">         <returnCurrencyNameReturn xsi:type="xsd:string">         Peso        </returnCurrencyNameReturn>      </returnCurrencyNameResponse>     </soapenv:Body>    </soapenv:Envelope> 

This is the expected SOAP response from the Web Service. By using a Get method from a HTML form we were able to communicate with the service.


When this XML comes back to Internet Explorer, you’ll get an error stating that you can’t have two root elements. This happens because executing a Get against an Axis Web Service, returns <p>Got response message</p> at the beginning of the request. This prevents Internet Explorer from parsing the XML correctly. To retrieve the XML, do a “View Source” and Internet Explorer loads the XML into Notepad, and then you can copy and paste it anywhere you’d like.


If you communicate with an Axis Web Service using the Post method, it expects to receive a complete SOAP request and this just won’t work within an HTML form.

This example shows how to communicate with Axis in a rudimentary fashion. The next section demonstrates how to accomplish the same thing with .NET.

Making a Request to an .NET Web Service with HTML

The previous example demonstrated how to communicate with an Axis Web Service. The following HTML communicates with the GetTestQuote .NET Web Service demonstrated in Chapter 6. Note that a .NET Web Service will communicate through an HTML form either with a Post or a Get request.

    <HTML>     <HEAD>        <TITLE>Test Web Service</TITLE>     </HEAD>     <FORM      METHOD="POST" ACTION=     "     /GetTestQuote">     Method Name:     <input type="text" name="symbol" value="C">     <BR>     <input type="submit">     </FORM>     </HTML> 

Figure 13.6 shows this HTML form in Internet Explorer.

click to expand
Figure 13.6: The HTML form that communicates with the.NET Web Service.

.NET Web Services detect that you are communicating from HTML because you’re not using a SOAP request. The .NET Web Services then create a much simpler response, as the following code illustrates.

     <?xml version="1.0" encoding="utf-8" ?>  <double xmlns="">55.95</double>

The previous code is a simple XML document that returns with simple elements describing the type of value. Note that in the .NET example the method is part of the URL rather than one of the inputs.

Using an Application to Communicate with Web Services

Java and C# contain built-in classes that make requests to Web pages and return results. These are useful for creating programs that monitor Web sites or act as your own personal spider. Almost all languages possess classes or libraries that allow you to write applications that communicate in this manner. Thus, you can use these implementations to communicate with a Web Service in a nonstandard but completely functional manner. The trick is to make the request via Get or Post from the application and then parse the response with whatever tools you have available (many include an XML parser at best and string-parsing methods at worst). Consider the following Java example.

    import*;     import*;     public class UseGet {        private URL myUrl;        private URLConnection myUrlConnection;        //set URL        public void setUrl(String sentUrl) {        try {          this.myUrl = new URL(sentUrl);        } catch  (MalformedURLException e) {          System.err.println(sentUrl +          " is not a useable URL");          System.err.println(e);        }       }        //begin the connection        public void createConnection() {         try {           this.myUrlConnection = myUrl.openConnection();         } catch (Exception e) {           System.err.println(e);         }        }        //Get content of the URL, return as string        public String getUrlContent() {          String currentLine = null;          String returnResponse = "";          try {           DataInputStream webResponse =           New DataInputStream          (myUrlConnection.getInputStream());           while (( currentLine = webResponse.readLine())               != null) {             returnResponse += currentLine;             returnResponse += "\n";           }          } catch(Exception e) {           System.err.println(e);         }          return(returnResponse);       }     public static void main(String args[]) {        UseGet myUseGet = new UseGet();        //set URL        String someURL =        "http://localhost:8080/axis/MoneyExchange.jws        ?method=returnCurrencyName&countryName=Swiss";        myUseGet.setUrl(someURL);        myUseGet.createConnection();        String UrlContent = myUseGet.getUrlContent();        System.out.println(UrlContent);     }

In the previous Java code, there is a request made to an Axis Web Service via the Get method. You know it’s a Get method because in the main of the program, the string someURL gets sent to the URL of the service and contains parameters in the query string to specify the method and the value of parameter to pass to the method. In this particular example, the example specifies returnCurrencyName as the method and the parameter countryName as the value to pass to the method.

Once the URL of the service is set with the setUrl method, the createConnection method sets up the connection to the desired URL. The getURLContent method grabs the content and puts it into the String UrlContent. Then the program prints out the following XML.

    <p>Got response message</p>     <?xml version="1.0" encoding="UTF-8"?>     <soapenv:Envelope       xmlns:soapenv=       ""       xmls:xsd=""       xmlns:xsi="">     <soapenv:Body>        <returnCurrencyNameResponse             soapenv:encodingStyle=            "">         <returnCurrencyNameReturn xsi:type="xsd:string">             Franc         </returnCurrencyNameReturn>        </returnCurrencyNameResponse>     </soapenv:Body>    </soapenv:Envelope>

We can also communicate this way with .NET Web Services using the following URL in the same Java program.

The response from the .NET Web Service once the program executes is then the same as when we contacted it with the HTML.

  <?xml version="1.0" encoding="utf-8"?>  <double   xmlns="">55.95</double>

Once the contents of the request make it back to the string UrlContent in the program, you can print it out, write it to a file, or parse it with either string or XML parsing. It all depends on what is available in your environment that doesn’t support a Web Services implementation.

Cross-Platform Web Services Using C# and Java
Cross-Platform Web Services Using C# & JAVA (Charles River Media Internet & Web Design)
ISBN: 1584502622
EAN: 2147483647
Year: 2005
Pages: 128 © 2008-2017.
If you may any questions please contact us: