webservice调用通过客户端编程的方式调用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>

文章结束给大家分享下程序员的一些笑话语录: Borland说我很有前途,Sun笑了;Sun说我很有钱,IBM笑了;IBM说我很专业,Sybase笑了;Sybase说我数据库很牛,Oracle笑了;Oracle说我是开放的,Linux笑了;Linux说我要打败Unix,微软笑了;微软说我的系统很稳定,我们都笑了。

--------------------------------- 原创文章 By
webservice和调用
---------------------------------

原文地址:https://www.cnblogs.com/xinyuyuanm/p/3112841.html