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


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值