通过客户端编程的方式调用webservice

通过客户端编程的方式调用webservice其实与通过jdk调用webservice的方式其实是一样的。在4种调用webservice的方式中推荐使用

通过jdk来调用webservice的那一种方式,因为其实他才是最简单的。

1、服务器端的代码和通过jdk调用webservice的方式是一样的。

2、客户端代码的书写

package com.njupt.webservice.client;

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import com.njupt.webservice.HelloService;

public class App {

	public static void main(String[] args) throws Exception {
		URL wsUrl = new URL("http://127.0.0.1:6790/hello?wsdl");
	    
		//QName()的第一个参数是wenservice(服务端)所在包的倒序,在此作为命名空间
		Service s = Service.create(wsUrl, new QName("http://webservice.njupt.com/", "HelloServiceService"));
		
		//注意,getPort()方法的第二个参数是HelloServicePort,二Service.create()的第二个参数是HelloServiceService.
		//不要写错了,二者都可以在这个webservice的wsdl文件中找到
		HelloService hs = s.getPort(new QName("http://webservice.njupt.com/","HelloServicePort"),HelloService.class);
		
		String ret = hs.sayHello("章泽天");
		
		System.out.println(ret);
	}
}


====================================================================================================================================

在最后附上这一个webservice的wsdl的代码

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<!--
 Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. 
-->
<!--
 Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. 
-->
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://webservice.njupt.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://webservice.njupt.com/" name="HelloServiceService">
<types>
<xsd:schema>
<xsd:import namespace="http://webservice.njupt.com/" schemaLocation="http://127.0.0.1:6790/hello?xsd=1"/>
</xsd:schema>
</types>
<message name="sayHello">
<part name="parameters" element="tns:sayHello"/>
</message>
<message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse"/>
</message>
<portType name="HelloService">
<operation name="sayHello">
<input message="tns:sayHello"/>
<output message="tns:sayHelloResponse"/>
</operation>
</portType>
<binding name="HelloServicePortBinding" type="tns:HelloService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="sayHello">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="HelloServiceService">
<port name="HelloServicePort" binding="tns:HelloServicePortBinding">
<soap:address location="http://127.0.0.1:6790/hello"/>
</port>
</service>
</definitions>


你可能感兴趣的:(通过客户端编程的方式调用webservice)