暑假web service 学习笔记2

soap-rpc
  it defines a model for representing an rpc and an rpc response using the soap
infrasture.
 what's really important is that soap defines a uniform model for
representing an rpc and its return valuee or values. the fundamental requirements for
an rpc all are that the body element contains the method name and the parameters and
that the parameters are accessible via accessors. in addition, soap has provisions for
encoding the mehtod signature ,header data, and the uri that represents the destination.

a remote procedure call

public static float sendSoapRPCMessage(String url,String isbc) throws Exception
{
org.apache.soap.rpc.Call call=new org.apche.soap.rpc.Call();
String encodingStyleURL=org.apache.soap.Constants.NS_URI_SOAP_ENC;
call.setEncodingStyleURI(encodingStyleURI);
call.setTargetObjectURI("urn:xmethods-BNPriceCheck");
call.setMethodName("getPrice");
Vector params=new Vector();
params.addElelment(new org.apache.soap.rpc.Parameter("isbn",String.class,isbn,null));
call.setParams(params);
org.apache.soap.rpc.Response resp=call.invoke(new java.net.URL(url),");
}

  another simple example

the client:
 
  import java.net.*;
import java.util.*;
public class CheckStock {
private static final String DEFAULT_HOST_URL
= "http://localhost:8080/soap/servlet/rpcrouter";
private static final String DEFAULT_ITEM = "Test";
private static final String URI = "urn:oreilly-jaws-samples";
//Member variables
private String m_hostURL;
private String m_item;
public CheckStock (String hostURL, String item) throws Exception
{
m_hostURL = hostURL;
m_item = item;
// print stuff to the console
...
}
public void checkStock( ) throws Exception {
//Build the call.
org.apache.soap.rpc.Call call = new org.apache.soap.rpc.Call ( );
//This service uses standard SOAP encoding
String encodingStyleURI = org.apache.soap.Constants.NS_URI_SOAP_ENC;
call.setEncodingStyleURI(encodingStyleURI);
//Set the target URI
call.setTargetObjectURI ("urn:stock-onhand");
//Set the method name to invoke
call.setMethodName ("getQty");
//Create the parameter objects
Vector params = new Vector ( );
params.addElement (new org.apache.soap.rpc.Parameter("item",
String.class, m_item, null));
//Set the parameters
call.setParams (params);
//Invoke the service
org.apache.soap.rpc.Response resp
= call.invoke ( new java.net.URL(m_hostURL),"urn:go-do-this");
//Check the response
if (resp != null) {
if (resp.generatedFault ( )) {
org.apache.soap.Fault fault = resp.getFault ( );
System.out.println ("Call failed due to a SOAP Fault: ");
System.out.println (" Fault Code = " + fault.getFaultCode());
Java Web Services
62
System.out.println("Fault String = " + fault.getFaultString());
} else {
org.apache.soap.rpc.Parameter result = resp.getReturnValue( );
Integer intresult = (Integer) result.getValue( );
System.out.println ("The stock-on-hand quantity for this item
is: " + intresult );
}
}
}
/** Main program entry point. */
public static void main(String args[]) {
// Command line parsing
...
// Start the CheckStock client
try
{
CheckStock stockClient = new CheckStock(hostURL, item);
stockClient.checkStock( );
}catch(Exception e){
System.out.println(e.getMessage( ));
}
}
...
}

the service:

  import java.net.*;
import java.util.*;
public class CheckStock {
private static final String DEFAULT_HOST_URL
= "http://localhost:8080/soap/servlet/rpcrouter";
private static final String DEFAULT_ITEM = "Test";
private static final String URI = "urn:oreilly-jaws-samples";
//Member variables
private String m_hostURL;
private String m_item;
public CheckStock (String hostURL, String item) throws Exception
{
m_hostURL = hostURL;
m_item = item;
// print stuff to the console
...
}
public void checkStock( ) throws Exception {
//Build the call.
org.apache.soap.rpc.Call call = new org.apache.soap.rpc.Call ( );
//This service uses standard SOAP encoding
String encodingStyleURI = org.apache.soap.Constants.NS_URI_SOAP_ENC;
call.setEncodingStyleURI(encodingStyleURI);
//Set the target URI
call.setTargetObjectURI ("urn:stock-onhand");
//Set the method name to invoke
call.setMethodName ("getQty");
//Create the parameter objects
Vector params = new Vector ( );
params.addElement (new org.apache.soap.rpc.Parameter("item",
String.class, m_item, null));
//Set the parameters
call.setParams (params);
//Invoke the service
org.apache.soap.rpc.Response resp
= call.invoke ( new java.net.URL(m_hostURL),"urn:go-do-this");
//Check the response
if (resp != null) {
if (resp.generatedFault ( )) {
org.apache.soap.Fault fault = resp.getFault ( );
System.out.println ("Call failed due to a SOAP Fault: ");
System.out.println (" Fault Code = " + fault.getFaultCode());
Java Web Services
62
System.out.println("Fault String = " + fault.getFaultString());
} else {
org.apache.soap.rpc.Parameter result = resp.getReturnValue( );
Integer intresult = (Integer) result.getValue( );
System.out.println ("The stock-on-hand quantity for this item
is: " + intresult );
}
}
}
/** Main program entry point. */
public static void main(String args[]) {
// Command line parsing
...
// Start the CheckStock client
try
{
CheckStock stockClient = new CheckStock(hostURL, item);
stockClient.checkStock( );
}catch(Exception e){
System.out.println(e.getMessage( ));
}
}
...
}

the deployment descriptor:
<isd:service xmlns:isd="http://xml.apache.org/xml-soap/deployment"
id="urn:stock-onhand">
<isd:provider type="java"
scope="Application"
methods="getQty">
<isd:java class="StockQuantity"/>
</isd:provider>
<isd:faultListener>org.apache.soap.server.DOMFaultListener
</isd:faultListener>
</isd:service>
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值