当前位置:编程学习 > JSP >>

SOAP教程(-)--客户端

答案:客户端,程序中做了很多的注释,看起来应该不会太困难,我就不多说了.
package demo;

/**
* Title:SOAP
* Description: 浆糊作品
* Copyright:    Copyright (c) 2001
* Company:
* @author 一桶浆糊
* @version 1.0
*/
import org.apache.soap.*;
import org.apache.soap.rpc.*;
import java.util.*;
import java.net.URL;
public class HelloClient {

  public HelloClient() {
  }
   public static void main( String[] args ) throws Exception
     {
     URL url = new URL( "http://localhost:8080/soap/servlet/rpcrouter" );
     String urn = "urn:demo:helloworld"; //服务器名称,在部署的时候自己定义

     Call call = new Call(); // 准备调用web service
     call.setTargetObjectURI( urn );
     call.setMethodName( "getHello" );
     call.setEncodingStyleURI( Constants.NS_URI_SOAP_ENC );
     Vector params = new Vector();
     //设置接口参数
     params.addElement( new Parameter("aUserName", String.class, "浆糊", null ) );
     call.setParams( params );

     try
       {
       System.out.println( "invoke service\n" + "  URL= " + url + "\n  URN =" + urn );
       Response response = call.invoke( url, "" ); // 进行调用

       if( !response.generatedFault() )  //是否产生错误
         {
         Parameter result = response.getReturnValue(); // 取得返回值
         
         System.out.println( "Result= " + result.getValue() );//哇!,成功了
         }
       else
         {
         Fault f = response.getFault(); // 得到一个错误
         System.err.println( "Fault= " + f.getFaultCode() + ", " + f.getFaultString() );
         }
       }
     catch( SOAPException e ) // 捕获异常
       {
       System.err.println( "SOAPException= " + e.getFaultCode() + ", " + e.getMessage() );
       }
     }
}

上一个:深入了解MIDP-基础篇(转贴)
下一个:SOAP教程 (-)

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,