【原】用SOAP訪問Web服務的一個例子
作者:lixiaosan
日期:24/May/2007
前言:
以下是一個通過用SOAP訪問遠程web服務,進行身分識別驗證的簡單例子。
該例描述了以下內容:
* 如何在服務端部署服務。
* 如何在用戶端構建HTTP request header 和 SOAP Message。
* 通過socket發送HTTP request, 並得到HTTP response。
註:因為說明文檔原來就是用E文寫的,難得改了,請各位見諒。
////////////////////////////////////////////////////////////////////////////////
// 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. 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 to
%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 includes two
items: ID and Password. Now Insert values to this table.
================================================================================
* Deploy the service
The server implementation includes 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 the
service, the following steps should 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 to
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.
////////////////////////////////////////////////////////////////////////////////
// Client-End
////////////////////////////////////////////////////////////////////////////////
================================================================================
* Construct HTTP header and SOAP Envelope
We use HTTP to send/receive SOAP message. So we should 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 should 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.executeQuery(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>