对web服务一无所知,我只是尝试调用一些由wsdl描述的"isAlive“服务。
对我来说,这似乎只需要2-5行代码,但我似乎找不到任何东西,但我似乎找不到涉及第三方软件包等的巨大的长示例。
有谁有什么想法吗?如果它总是被认为是很长的,那么为什么它必须如此复杂,也许一个很好的解释也会受到赞赏。我使用的是Eclipse,而wsdl是SOAP。
发布于 2009-12-01 22:38:38
JDK 6附带了jax-ws,这是为web服务开发客户端所需的一切。
我找不到一些足够简单的例子来发布,但从https://jax-ws.dev.java.net/开始
编辑:这里有一个简单的示例-此web服务的客户端:http://xmethods.com/ve2/ViewListing.po?key=427565
C:\temp> md generated
C:\temp>"c:\Program Files\Java\jdk1.6.0_17"\bin\wsimport -keep -d generated http://www50.brinkster.com/vbfacileinpt/np.asmx?wsdl创建如下所示的PrimeClient.java:
import javax.xml.ws.WebServiceRef;
import com.microsoft.webservices.*;
//the above namespace is from the generated code from the wsdl.
public class PrimeClient {
//Cant get this to work.. @WebServiceRef(wsdlLocation="http://www50.brinkster.com/vbfacileinpt/np.asmx?wsdl")
static PrimeNumbers service;
public static void main(String[] args) {
try {
service = new PrimeNumbers();
PrimeClient client = new PrimeClient();
client.doTest(args);
} catch(Exception e) {
e.printStackTrace();
}
}
public void doTest(String[] args) {
try {
System.out.println("Retrieving the port from the following service: " + service);
PrimeNumbersSoap pm = service.getPrimeNumbersSoap();
System.out.println("Invoking the getPrimeNumbersSoap operation ");
System.out.println(pm.getPrimeNumbers(100));
} catch(Exception e) {
e.printStackTrace();
}
}
} 编译并运行:
C:\temp>"c:\Program Files\Java\jdk1.6.0_17"\bin\javac -cp generated PrimeClient.java
C:\temp>"c:\Program Files\Java\jdk1.6.0_17"\bin\java -cp .;generated PrimeClient
Retrieving the port from the following service: com.microsoft.webservices.PrimeN
umbers@19b5393
Invoking the getPrimeNumbersSoap operation
1,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97发布于 2009-12-01 22:24:26
IDE提供了一些插件,可以生成所需的代码来为您使用web服务。
在插件生成基本方法后,您只需像这样调用web服务:
TransportServiceSoap service = new TransportServiceLocator().getTransportServiceSoap();
service.getCities();看一看http://urbas.tk/index.php/2009/02/20/eclipse-plug-in-as-a-web-service-client/
发布于 2009-12-01 23:05:54
There are three ways to write a web service client
(DII)
动态代理客户端的示例
import java.net.URL;
import javax.xml.rpc.Service;
import javax.xml.rpc.JAXRPCException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceFactory;
import dynamicproxy.HelloIF;
public class HelloClient {
public static void main(String[] args) {
try {
String UrlString = "Your WSDL URL"; //
String nameSpaceUri = "urn:Foo";
String serviceName = "MyHelloService";
String portName = "HelloIFPort";
System.out.println("UrlString = " + UrlString);
URL helloWsdlUrl = new URL(UrlString);
ServiceFactory serviceFactory =
ServiceFactory.newInstance();
Service helloService =
serviceFactory.createService(helloWsdlUrl,
new QName(nameSpaceUri, serviceName));
dynamicproxy.HelloIF myProxy =
(dynamicproxy.HelloIF)
helloService.getPort(
new QName(nameSpaceUri, portName),
dynamicproxy.HelloIF.class);
System.out.println(myProxy.sayHello("Buzz"));
} catch (Exception ex) {
ex.printStackTrace();
}
}
} I hope , this would solve your question.
https://stackoverflow.com/questions/1826379
复制相似问题