是否可以使用HTTP客户端调用Web服务?
如果是,请给我一些例子。如何获取web服务中方法列表?
例如:
我正在使用这个Web Service WSDL link
它有两个函数FahrenheitToCelsius和CelsiusToFahrenheit
注意:我知道如何使用Web客户端调用webservice,但我需要使用HTTP客户端执行调用webService
发布于 2012-09-20 21:16:49
只要web服务是由HTTP协议公开的,它就是肯定的。但是您必须自己解析响应,并自己构造有效的请求。使用像Apache Axis这样的框架要容易得多,因为它将所有这些都自动化了。
您还应该注意,此web服务正在使用SOAP协议,当您尝试使用它时,应该考虑到这一点。
发布于 2012-09-20 22:22:02
可以,停那儿吧。例如,使用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);
}
}对于方法,请查看WSDL文件中名为operation的元素。
https://stackoverflow.com/questions/12513362
复制相似问题