0

有 java servlet,它包含两个带有货币缩写的字符串变量。有提供货币汇率的外部 JAX-WS 服务http://www.webservicex.net/ws/WSDetails.aspx?CATID=2&WSID=10 。如何向该服务提出请求?如何将这两个字符串变量发送给他并取回数字?使用 Eclipse EE Kepler、Tomcat 6

4

1 回答 1

1

对于www.webservicex.net中的某些服务,您可以使用 HTTP 进行 GET 请求。例如:

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    String from = "USD";
    String to = "EUR";

    java.net.URL url = new java.net.URL(
            "http://www.webservicex.net/CurrencyConvertor.asmx"
                    + "/ConversionRate?FromCurrency=" + from
                    + "&ToCurrency=" + to);
    java.util.Scanner sc = new java.util.Scanner(url.openStream());

    // <?xml version="1.0" encoding="utf-8"?>
    sc.nextLine();

    // <double xmlns="http://www.webserviceX.NET/">0.724</double>
    String str = sc.nextLine().replaceAll("^.*>(.*)<.*$", "$1");

    sc.close();

    Double rate = Double.parseDouble(str);
    log("Rate: " + rate);

}
于 2013-10-24T13:56:49.397 回答