[Original] An example of using soap to access web services
Author: lixiaosan
Date: 24, May/2007
Preface:
The following is a simple example of using soap to access remote Web Services for identity authentication.
This example describes the following:
* How to deploy services on the server.
* How to create an HTTP request header and a SOAP message on the client.
* Send an HTTP request through socket and obtain the HTTP response.
Note: Since the instruction document was originally written in E, it is rare to change it. Please forgive me.
//////////////////////////////////////// ////////////////////////////////////////
// Server-end
//////////////////////////////////////// ////////////////////////////////////////
========================================================== ==========================================================
* Platform
Windows XP
========================================================== ==========================================================
* Software:
Jdk-1_5_0-windows-i586.exe
Jre-1_5_0-windows-i586.exe
Apache-tomcat-5.5.20.exe
Xerces-J-bin.1.4.4.zip
Axis-bin-1.4.tar
Mysql-5.0.27-win32.zip
Mysql-connector-java-5.0.4.zip
Note: These softwares are free and can be found and downloaded from Internet.
========================================================== ==========================================================
* Setup system environment parameters
Java_home = C:/program files/Java
Tomcat_home = C:/program files/Apache Software Foundation/tomcat 5.5
Tomcat_common_lib = % tomcat_home %/common/lib
Axis_home = % tomcat_home %/webapps/axis
Axis_lib = % axis_home %/WEB-INF/lib
Axisclasspath = % axis_lib %/axis. jar;
% Axis_lib %/axis-ant.jar;
% Axis_lib %/commons-discovery-0.2.jar;
% Axis_lib %/commons-logging-1.0.4.jar;
% Axis_lib %/jaxrpc. jar;
% Axis_lib %/SAAJ. jar;
% Axis_lib %/log4j-1.2.8.jar;
% Axis_lib %/wsdl4j-1.5.1.jar;
Classpath = % tomcat_common_lib %/xerces. jar;
% Tomcat_common_lib %/activation. jar;
% Tomcat_common_lib %/mysql-connector-java-5.0.4-bin.jar;
% Axisclasspath %;
% Axis_home %;
% Java_home %/jdk1.5.0/lib/dt. jar;
% Java_home %/jdk1.5.0/lib/tools. jar:
% Java_home %/jre1.5.0 _ 10/lib/RT. Jar
========================================================== ==========================================================
* Install software to server
1. Install JDK and JRE.
2. install Apache Tomcat. (default to % tomcat_home %)
3. The unpack Xerces-J-bin.1.4.4.zip and copy xerces. jar to % tomcat_common_lib % directory.
4. Unpack axis-bin-1.4.tar and copy axis-1_4/webapps/axis
% Tomcat_home %/webapps/directory.
5. Install mysql-5.0.27-win32.
6. Unpack mysql-connector-java-5.0.4.zip and copy mysql-connector-java-5.0.4-bin.jar
To % tomcat_common_lib %/directory.
7. Create a database named mydb and create a table named mytable which between des two
Items: ID and password. Now insert values to this table.
========================================================== ==========================================================
* Deploy the service
The server implementation implements des 5 files:
Deploy_monitor.wsdd
Undeploy_monitor.wsdd
Authenticateservice. Java
Authenticateservice. Class
Authenticateservice. WSDL
Note: the detail of these files are shown in part appendix.
The steps of deploying service are shown as follows:
1. Create a folder named authentication under % axis_home %/WEB-INF/classes/Samples
And copy the 5 files above to this new folder.
2. compile.
Open a window and run:
Cd % axis_home %/WEB-INF/classes/samples/Authentication
Javac *. Java
Then a new file authenticateservice. class will be generated.
3. Start a server.
Run:
Java org. Apache. axis. Transport. http. simpleaxisserver-P 8080
4. Deploy the service.
To deploy the service, run:
Java org. Apache. axis. Client. adminclient deploy_monitor.wsdd
5. redeploy the service.
If you have changed authenticateservice. Java, you should redeploy
Service, the following steps shocould be done:
(1) undeploy the service, run:
Java org. Apache. axis. Client. adminclient undeploy_monitor.wsdd
(2) Stop Tomcat service.
(3) start Tomcat service.
(4) Step 2.
(5) Step 3.
(6) Step 4.
========================================================== ==========================================================
* Soap Monitor
The goal of the soap monitor utility is to provide a way for these developers
Monitor the soap messages being used without requiring any special configuration
Or restarting of the server.
About detail of soap monitor, see at http://ws.apache.org/axis/java/user-guide.html
========================================================== ==========================================================
* Generate WSDL File
To generate a WSDL file, run:
Java org. Apache. axis. WSDL. java2wsdl-O authenticateservice. WSDL
-L "http: // 192.168.13.4: 8080/axis/services/authenticateservice"
-N "authenticate" samples. Authentication. authenticateservice
Note: 192.168.13.4 is the Server IP address.
//////////////////////////////////////// ////////////////////////////////////////
// Client-end
//////////////////////////////////////// ////////////////////////////////////////
========================================================== ==========================================================
* Construct HTTP header and SOAP envelope
We use HTTP to send/receive SOAP message. So we shoshould construct the HTTP Header
And SOAP message.
Post/axis/services/authenticateservice HTTP/1.1
HOST: 192.168.13.4
Content-Type: Application/soap + XML; charset = UTF-8
Content-Length: XXX
Connection: Close
<? XML version = "1.0" encoding = "UTF-8"?>
<SOAP-ENV: envelope xmlns: SOAP-ENV = "http://www.w3.org/2003/05/soap-envelope"
Xmlns: SOAP-ENC = "http://www.w3.org/2003/05/soap-encoding"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: XSD = "http://www.w3.org/2001/XMLSchema"
Xmlns: NS1 = "process">
SOAP-ENV: Body>
<NS1: Process SOAP-ENV: encodingstyle = "http://schemas.xmlsoap.org/soap/encoding/">
<In0> 001 </in0>
<In1> testpsd </in1>
</NS1: Process>
SOAP-ENV: Body>
SOAP-ENV: envelope>
Note:
1. When you copy the content above to a buffer, you should add "/R/n" to the end
Of each line of HTTP header. There are no blank between tags in SOAP envelope
Following HTTP header.
2. The value of Content-Length field is the length of SOAP envelope not including
HTTP header. You shoshould place an Valid String instead of "XXX" shown above.
3. About HTTP header, see rfc2616 and soap 1.2 specification.
========================================================== ==========================================================
* Connect to server and send/receive HTTP message
To connect to server and send/receive HTTP message, the easiest way is via socket.
Connect-> send-> Recv
//////////////////////////////////////// ////////////////////////////////////////
* Appendix
//////////////////////////////////////// ////////////////////////////////////////
1. authenticateservice. Java
// Authenticateservice. Java
Package samples. authentication;
// SQL
Import java. SQL. connection;
Import java. SQL. drivermanager;
Import java. SQL. sqlexception;
Import java. SQL. statement;
Import java. SQL. resultset;
Public class authenticateservice
{
Public String process (string userid, string userpassword) throws exception
{
String Strid = NULL;
String strpassword = NULL;
String strret = NULL;
String strsql = NULL;
Try
{
Class. forname ("com. MySQL. JDBC. Driver"). newinstance ();
}
Catch (exception ex)
{
;
}
Try
{
String url = "JDBC: mysql: // localhost/mydb? User = root & Password = 123456 ";
Connection conn = drivermanager. getconnection (URL );
Statement stmt = NULL;
Resultset rsult = NULL;
Stmt = conn. createstatement ();
Strsql = string. Format ("select * From mytable where id = '% S'", userid );
Rsult = stmt.exe cutequery (strsql );
If (! Rsult. Next ())
{
Strret = "User ID is not exist! ";
}
Else
{
Strpassword = rsult. getstring ("password ");
If (strpassword. Equals (userpassword ))
{
Strret = "the authentication is successful! ";
}
Else
{
Strret = "password is error! ";
}
}
}
Catch (sqlexception ex)
{
// Handle any errors
System. Out. println ("sqlexception:" + ex. getmessage ());
System. Out. println ("sqlstate:" + ex. getsqlstate ());
System. Out. println ("vendorerror:" + ex. geterrorcode ());
Strret = "SQL _error ";
}
Return strret;
}
}
2. deploy_monitor.wsdd
<! -- Deploy_monitor.wsdd -->
<Deployment name = "test" xmlns = "http://xml.apache.org/axis/wsdd"
Xmlns: Java = "http://xml.apache.org/axis/wsdd/providers/java">
<Service name = "authenticate" provider = "Java: RPC">
<Parameter name = "classname" value = "samples. Authentication. authenticateservice"/>
<Parameter name = "allowedmethods" value = "process"/>
<Parameter name = "allowedroles" value = "user3"/>
<! -- The following code is used to monitor SOAP message via soap monitor -->
<Requestflow>
<Handler type = "soapmonitor"/>
</Requestflow>
<Responseflow>
<Handler type = "soapmonitor"/>
</Responseflow>
</Service>
</Deployment>
3. undeploy_monitor.wsdd
<Undeployment name = "test" xmlns = "http://xml.apache.org/axis/wsdd/">
<Service name = "authenticate"/>
</Undeployment>