Mike Voitenko
Mike Voitenko

Reputation: 133

Using web services from http://www.webservicex.net with Java

There is java servlet, which contains two String variables with currency abbreviations. There is external JAX-WS service http://www.webservicex.net/ws/WSDetails.aspx?CATID=2&WSID=10 which gives currency rates. How to make request to this service? How to send him this two string variables and get number back? Using Eclipse EE Kepler, Tomcat 6

Upvotes: 0

Views: 2416

Answers (1)

Paul Vargas
Paul Vargas

Reputation: 42020

For some services in www.webservicex.net, you can do a GET request using HTTP. e.g.:

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);

}

Upvotes: 1

Related Questions