java汇率webservice服务

//一个不错的例子
//需要soap.jar
import java.io.*;
import java.net.*;
import java.util.*;
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.rpc.*;

public class CurrencyClient {

        public static float getRate(URL url, String country1, String country2)
                        throws Exception {

                Call call = new Call();

                // Set encoding style. Use the standard SOAP encoding
                String encodingStyleURI = Constants.NS_URI_SOAP_ENC;
                call.setEncodingStyleURI(encodingStyleURI);

                // Set service locator parameters
                call.setTargetObjectURI("urn:xmethods-CurrencyExchange");
                call.setMethodName("getRate");

                // Create the input parameter vector
                Vector params = new Vector();
                params.addElement(new Parameter("country1", String.class, country1,
                                null));
                params.addElement(new Parameter("country2", String.class, country2,
                                null));
                call.setParams(params);

                // Invoke the service ...
                Response resp = call.invoke(url, "");

                // ... and evaluate the result
                if (resp.generatedFault()) {
                        throw new Exception();
                } else {

                        // Call was succesfull. Extract response parameter and return
                        Parameter result = resp.getReturnValue();
                        Float rate = (Float) result.getValue();
                        return rate.floatValue();
                }
        }

        // Driver to illustrate invocation of service
        public static void main(String[] args) {
                try {
                        URL url = new URL("http://services.xmethods.com:80/soap");
                        String country1 = "us";
                        String country2 = "china";
                        float rate = getRate(url, country1, country2);
                        System.out.println(rate);
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
}
当前结果是8.098

 

 

 

 

 

 

 

 

 

 

 

<!--服务描述-->
<?xml version="1.0"?>
<definitions name="CurrencyExchangeService" targetNamespace="http://www.xmethods.net/sd/CurrencyExchangeService.wsdl" xmlns:tns="http://www.xmethods.net/sd/CurrencyExchangeService.wsdl" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns="http://schemas.xmlsoap.org/wsdl/">
        <message name="getRateRequest">
                <part name="country1" type="xsd:string"/>
                <part name="country2" type="xsd:string"/>
        </message>
        <message name="getRateResponse">
                <part name="Result" type="xsd:float"/>
        </message>
        <portType name="CurrencyExchangePortType">
                <operation name="getRate">
                        <input message="tns:getRateRequest" />
                        <output message="tns:getRateResponse" />
                </operation>
        </portType>
        <binding name="CurrencyExchangeBinding" type="tns:CurrencyExchangePortType">
                <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
                <operation name="getRate">
                        <soap:operation soapAction=""/>
                        <input >
                                <soap:body use="encoded" namespace="urn:xmethods-CurrencyExchange" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
                        </input>
                        <output >
                                <soap:body use="encoded" namespace="urn:xmethods-CurrencyExchange" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
                        </output>
                </operation>
        </binding>
        <service name="CurrencyExchangeService">
                <documentation>Returns the exchange rate between the two currencies</documentation>
                <port name="CurrencyExchangePort" binding="tns:CurrencyExchangeBinding">
                        <soap:address location="http://services.xmethods.net:80/soap"/>
                </port>
        </service>
</definitions>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

光义

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值