SOAP教程(-)--客户端
发表于:2007-07-01来源:作者:点击数:
标签:
客户端,程序中做了很多的注释,看起来应该不会太困难,我就不多说了. package demo; /** * Title: SOA P * Description: 浆糊作品 * Copyright:Copyright (c) 2001 * Company: * @author 一桶浆糊 * @version 1.0 */ import org.apache.soap.*; import org.apac
客户端,程序中做了很多的注释,看起来应该不会太困难,我就不多说了.
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( Const
ants.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() );
}
}
}
原文转自:http://www.ltesting.net