原创  【原】用SOAP访问Web服务的一个例子 收藏

 【原】用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>

发表于 @ 2007年05月24日 17:55:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:WSDL : 描述你的Web服务 | 新一篇:【转】杀死已知应用程序名的进程

  • 发表评论
  • 评论内容:
  •  
Copyright © lixiaosan
Powered by CSDN Blog