Bucks
Bucks

Reputation: 687

HTTP WebService

is it possible to call Web service by using HTTP Client ?

if yes give me some examples. How can i get list of Methods present in that web service?

for example :

I am using this Web Service WSDL link

it has two functions FahrenheitToCelsius and CelsiusToFahrenheit

Note : i know how to call webservice by using Web Client but i need to perform call webService by using HTTP Client

Upvotes: 0

Views: 293

Answers (2)

Stephan Wack
Stephan Wack

Reputation: 21

Yes, you can. E.g. with Apache HttpClient 4.2.1.

import java.io.File;
import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.fluent.Content;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;

public class HttpClientPost {
    public static void main(String[] args) throws ClientProtocolException, IOException {
        String request = "<soapenv:Envelope response xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
            "xmlns:tem=\"http://tempuri.org/\"><soapenv:Header/><soapenv:Body>" +
            "<tem:CelsiusToFahrenheit><tem:Celsius>100</tem:Celsius>" +
            "</tem:CelsiusToFahrenheit></soapenv:Body></soapenv:Envelope>";
        Content response = Request.Post("http://www.w3schools.com/webservices/tempconvert.asmx")
            .bodyString(request, ContentType.TEXT_XML).execute().returnContent();
        System.out.println("response: " + response);
    }
}

For the methods look at the elements named operation within the WSDL file.

Upvotes: 2

user684934
user684934

Reputation:

It sure is, as long as the web service is exposed by the HTTP protocol. But you'd have to parse the response yourself, and construct valid requests yourself. Much easier to use a framework like Apache Axis, which has all of this automated.

You should also note that this web service is using the SOAP protocol, which should be accounted for when you're trying to use it.

Upvotes: 0

Related Questions