This article describes a simple universal SOAP client that uses Java not using special SOAP libraries. The client allows you to create your own request with any XML editor (or text editor) instead of creating a SOAP request XML document for you in secret. The client displays the actual SOAP response XML document, not only the return value of the remote method. This short Java program is exactly what SOAP: Opens the HTTP connection, send the appropriate XML to call the remote method, then read the XML response returned by the server.
GM Java SOAP client
SOAP (Simple Object Access Protocol) is an IBM, Microsoft, Developmentor, and Userland Software developed to exchange information on the network, which has been developed W3C standard. As the SOAP server that can be disclosed on the web, SOAP is almost written in any language - even with popular simple languages (such as Visual Basic, JavaScript and Perl), very short programs - Execute HTML doing things for web browsers: It provides a simple way for these programs to use the available information sources on the World Wide Web.
Similar to HTML, SOAP provides a set of markers to represent different information blocks of using HTTP transport protocols on the Web (from SOAP 1.1, SMTP). However, SOAP is far from HTML to HTML. With SOAP, your program sends "SOAP Request" to the SOAP server (a short XML document describing the methods to be called on the remote machine and all parameters to be passed). The SOAP server will try to perform this method with those parameters and send the SOAP response to the program. The response can be the result of the execution, or the corresponding error message. You can use the public SOAP server to provide stock prices for clients, the latest currency exchange rate, FEDEX package tracking information, algebraic expressions, and other types of information.
Before SOAP exists, the program that attempts to use this information must first capture the web page, then "scrape" HTML to find the appropriate text. Detaching of these web pages (for example, putting the current stock price in the third column in the table), not the second column, can make these programs useless. The SOAP specification and the brief SOAP requests and response modes it bring provide a framework for the contact between the client and the server, which is the basis for those with strong information collection tools.
There are many SOAP clients to be used in most popular programming languages; for detailed lists, see the SOAP Toolkits section on the SOAP :: Lite for Perl home page (see Resources). Most SOAP clients provide class libraries, COM objects, or equivalents from your own programs. Typically, use these client libraries to follow the following modes:
· The name of the remote method to be called and all required parameters should be called.
· The appropriate XML documentation for the library assembly SOAP requests to pack this information.
· The library passes this XML document to the SOAP server identified by the SOAP endpoint URL, which is very similar to the user's address to the web server address with the URL of the specified server.
· After the SOAP server tries to execute the method, it assembles the SOAP response XML document containing execution results and send it back to the SOAP client.
· When receiving the SOAP response, the client library performs syntax analysis of XML to obtain the result of the method call, and passes the result to the progral of the library.
SOAPCLIENT4XG
SOAP introduction (see the "Web Service Revolution" column written on developerWorks on developerWorks, the "Web Service Revolution" column is always discussed for the XML structure used for SOAP requests and responses, but I have contacted the SOAP client always in the XML. Assembly and grammar analysis, I never know. As a person who uses XML, I once thought I did an XML section; I think soap is simple, then I should be able to write a simple SOAP client to read the XML document requesting the SOAP request, send it to the command line specified. SOAP endpoint URL, read back response documentation and output the response. This will make it a real universal SOAP client because it calls any method on any SOAP server. The SOAPCLIENT4XG ("SOAP Client for XML Geeks") Java class displayed in Listing 1 performs this task without using any dedicated Java SOAP class listed in the SOAP Toolkits page mentioned in the morning (see Resources). After checking the necessary SOAP endpoint URL and SOAP XML document file name parameters and optional SOAP operation parameters, read the file, send it to the SOAP server, read the response, and output it to the standard exit. Listing 1
Listing 1. The complete SOAP client / ** * SOAPClient4XG. Read the SOAP envelope file passed as the second * parameter, pass it to the SOAP endpoint passed as the first parameter, and * print out the SOAP envelope passed as a response. With help from Michael * Brennan 03/09/01 * * * @author Bob DuCharme * @version 1.1 * @param SOAPUrl URL of SOAP Endpoint to send request. * @param xmlFile2Send A file with an XML document of the request. * * 5 / 23/01 Revision: soapaction added * / import java.io. *; Import java.net. *; Public class soapclient4xg {public static void main (string [] args) throws exception {ix (args.length <2) { System.err.println ("Usage: Java SOAPCLIENT4XG" "http: // soaparch soapenvelopefile.xml" "[soapaction]"); System.err.Println ("soapaction is optional."); System.exit (1 ); String soapurl = args [0]; string xmlfile2send = args [1]; string s OAPAction = ""; if (args.length> 2) soapAction = args [2]; // Create the connection where we're going to send the file. URL URL = New URL; URLConnection Connection = URL.OpenConnection (); Httpurlconnection httpconn = (httpurlconnection) Connection; // Open theinnection. After We Copy It to a byte array, we can see //how big it is so what we can set the http cotent-length //property. (See Complete E-mail Below for more on this.) Fileinputstream fin = new fileinputstream (XMLFile2send); ByteArrayoutputStream Bout = New ByteArrayoutputStream ();
// copy the soap file to the open connection. Copy (FIN, BOUT); Fin.close (); Byte [] b = bout.tobyteaRray (); // 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 ", SOAPAction); httpConn.setRequestMethod ("POST"); httpconn.setdooutput (true); httpconn.setdoinput (true); // everyhes's set up; send the xml what was read in to b. OutputStream out = httpconn.getoutputstream (); out.write (B ); out.close (); // Read the response and write it to standard out InputStreamReader isr = new InputStreamReader (httpConn.getInputStream (.)); BufferedReader in = new BufferedReader (isr); String inputLine; while ((inputLine = IN.Readline ())! = null) SY stem.out.println (inputLine); in.close ();} // copy method from From ER Harold's book "Java I / O" public static void copy (InputStream in, OutputStream out) throws IOException {// do not allow other threads to read from the // input or write to the output while copying is // taking place 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);}}}}}
Because the SOAP client sends an XML SOAP request with the HTTP protocol, the work that must be done is HTTP settings. Java provides an HTTPURLCONNECTION class that has many "settings" methods to properly set each HTTP parameter and can set most of the parameters with a simple string. One HTTP parameter that requires a little extra code is Content-length, so SOAPCLIENT4XG calculates the length of the XML request by placing it in a byte array after reading the XML request, and then check the length characteristics of the byte array. You can use other HTTP implementations that will set these HTTP parameters on behalf of you. Sun Open Source Brazil Web Application Frame (see Refigu) automatically handles HTTP issues, and makes it easier to handle proper SOAP errors because (unlike HTTPURLCONNECTION Class) It is a specific writing to use Java A small application mitigates a universal HTTP class that is loaded into images and other web resources.
Run it
XMethods.com (see Resources) Provides a list of continuous development of public available SOAP services. In addition to telling you that the options for SOAP operation parameters are required for each service, many of their descriptions also include sample XML requests, so I copied sample requests for the temperature SOAP server, add some blank space, and in zipcode Elements replaced their own postal codes, as shown in Listing 2. Listing 2
Listing 2. SOAP XML Request to Find Out The Temperature At Zip Code 11217
This file is stored in a file named WeattherReq.xml, the command line in Listing 3 sends its content to the SOAP endpoint URL specified on the same XMethods web page of the sample XML
Listing 3 Listing 3. Using soapclient4xg to send the soap request java soapclient4xg http://services.xmeth 4.net:80/soap/servlet/rpcrouter weatherReq.xml
As shown in Listing 4, the SOAP server sends back the SOAP response, where the current temperature is stored in the response element. Listing 4
Listing 4. Temperature SOAP Server's Response XML Version = '1.0' encoding = 'UTF-8'?>