您的位置:首页 > 其它

WebService------------WebService客户端的四种调用方式

2016-09-14 00:00 483 查看
首先使用wsimport命令生成客户端调用代码

1.WSDL调用方法

第一步:阅读wsdl,搞清楚服务视图 service、 协议格式binding服务端点porttype

第二步:创建服务视图

第三步:创建服务端点

第四步:通过服务端点调用服务方法

实例

//创建服务视图
MobileCodeWS mobileCodeWS = new MobileCodeWS();
//获取服务实现类
MobileCodeWSSoap mobileCodeWSSoap = mobileCodeWS.getPort(MobileCodeWSSoap.class);
//调用查询方法
String reuslt = mobileCodeWSSoap.getMobileCodeInfo("13888888", null);
System.out.println(reuslt);

2.service调用方法【常用】

第一步:创建URL,指定资源地址即wsdl地址

第二步:创建Qname,指定命名空间和视图名称

第三步:创建服务视图对象service

第四步:从服务视图中得到服务端点即服务接口(这里需要服务接口类型,可使用wsimport生成后只留下porttype)

第五步:通过服务端点调用服务方法

实例:

//创建WSDL的URL,注意不是服务地址
URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");
//创建服务名称
//1.namespaceURI - 命名空间地址
//2.localPart - 服务视图名
QName qname = new QName("http://WebXml.com.cn/", "MobileCodeWS");
//创建服务视图
//参数解释:
//1.wsdlDocumentLocation - wsdl地址
//2.serviceName - 服务名称
Service service = Service.create(url, qname);
//获取服务实现类
MobileCodeWSSoap mobileCodeWSSoap = service.getPort(MobileCodeWSSoap.class);
//调用查询方法
String result = mobileCodeWSSoap.getMobileCodeInfo("1866666666", "");
System.out.println(result);

3.HTTPUrlConnent调用

ublic class HttpClient {

public static void main(String[] args) throws IOException {
//第一步:创建服务地址,不是WSDL地址
URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");
//第二步:打开一个通向服务地址的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//第三步:设置参数
//3.1发送方式设置:POST必须大写
connection.setRequestMethod("POST");
//3.2设置数据格式:content-type
connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
//3.3设置输入输出,因为默认新创建的connection没有读写权限,
// connection.setDoInput(true);
// connection.setDoOutput(true);

//第四步:组织SOAP数据,发送请求
String soapXML = getXML("15226466316");
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes());
//第五步:接收服务端响应,打印
int responseCode = connection.getResponseCode();
if(200 == responseCode){//表示服务端响应成功
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);

StringBuilder sb = new StringBuilder();
String temp = null;
while(null != (temp = br.readLine())){
sb.append(temp);
}
System.out.println(sb.toString());

is.close();
isr.close();
br.close();
}

os.close();
}

/**
* <?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<getMobileCodeInfo xmlns="http://WebXml.com.cn/">
<mobileCode>string</mobileCode>
<userID>string</userID>
</getMobileCodeInfo>
</soap:Body>
</soap:Envelope>
* @param phoneNum
* @return
*/
public static String getXML(String phoneNum){
String soapXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+"<soap:Body>"
+"<getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">"
+"<mobileCode>"+phoneNum+"</mobileCode>"
+"<userID></userID>"
+"</getMobileCodeInfo>"
+"</soap:Body>"
+"</soap:Envelope>";
return soapXML;
}

4.AJAX调用方式

function queryMobile(){
//创建XMLHttpRequest对象
var xhr = new XMLHttpRequest();
//打开连接
xhr.open("post","http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx",true);
//设置数据类型
xhr.setRequestHeader("content-type","text/xml;charset=utf-8");
//设置回调函数
xhr.onreadystatechange=function(){
//判断是否发送成功和判断服务端是否响应成功
if(4 == xhr.readyState && 200 == xhr.status){
alert(xhr.responseText);
}
}
//组织SOAP协议数据
var soapXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+"<soap:Body>"
+"<getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">"
+"<mobileCode>"+document.getElementById("phoneNum").value+"</mobileCode>"
+"<userID></userID>"
+"</getMobileCodeInfo>"
+"</soap:Body>"
+"</soap:Envelope>";
alert(soapXML);
//发送数据
xhr.send(soapXML);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息