准备好jdk6/bin/wsimport命令,该命令作用是:将本地或网上的WSDL文档转成java源码类和字节码
复制该地址:服务说明->http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL
cmd中输入:
e:/>wsimport -s . -p cn.go.test00 http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL
查看:e:/cn/ca/test00/*.class和*.java文件
-s表示生产源代码和字节码
.表示存放在当前目录下
-p表示为生成的源代码设置一个包名,提倡用客户端包名,例如:cn.ca.test06
http..表示第三方网站webservice服务的WSDL文件
4)将生成的java源代码复制到IDE开发工具中的客户端
5)写一个客户端类
public static void main(String[] args){
//创建服务对象
MobileCodeWS ws = new MobileCodeWS();
//选择调用方式:例如GET/POST/【SOAP1.1-JDK6支持】/SOAP1.2,其实返回是的动态代理对象,该对象实现了接口
MobileCodeWSSoap wsSOAP = ws.getMobileCodeWSSoap();
//调用服务
String returnValue = wsSOAP.getMobileCodeInfo("15876541234","");
//显示结果
System.out.println(returnValue);
}
八)基于java-httpclient程序,以HTTP协议GET和POST方式访问http://www.webxml.com.cn网站,查询一些公共的webservice服务【test04】
1)apache--HttpClient第三方工具类简介,不用借助真正的浏览器,通过HttpClient工具类,访问外网
前提:导入httpclient-jar包
常用类简介
HttpClient ie = new HttpClient()创建浏览器
GetMethod getMethod = new GetMethod(url)准备以GET方式提交数据到指定的URL路径
PostMethod postMethod = new PostMethod(url)准备以POST方式提交数据到指定的URL路径
GetMethod.setRequestHeader("请求头","内容")设置请求头内容
PostMethod.setRequestHeader("请求头","内容")设置请求头内容
int code = ie.executeMethod(getMethod)真正以GET方式提交数据到指定的URL路径,并返回响应状态码
int code = ie.executeMethod(postMethod)真正以POST方式提交数据到指定的URL路径,并返回响应状态码
PostMethod.setParameter("参数名","参数值")在POST方式方式下,为请求体设置值
getMethod.getResponseBodyAsString()获取响应结果,并转成字符串
postMethod.getResponseBodyAsString()获取响应结果,并转成字符串
HTTP协议/GET方式:
String url = "http://webservice.webxml.com.cn" +
"/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode="+phone+"&userID=";
HttpClient ie = new HttpClient();
GetMethod getMethod = new GetMethod(url);
getMethod.setRequestHeader("content-type","text/xml;charset=utf-8");
int code = ie.executeMethod(getMethod);
System.out.println(code==200?"成功":"失败");
String returnValue = getMethod.getResponseBodyAsString();
System.out.println(returnValue);
HTTP协议/POST方式:
String url = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
HttpClient ie = new HttpClient();
PostMethod postMethod = new PostMethod(url);
postMethod.setParameter("mobileCode",phone);
postMethod.setParameter("userID","");
postMethod.setRequestHeader("content-type","application/x-www-form-urlencoded");
int code = ie.executeMethod(postMethod);
System.out.println(code==200?"成功":"失败");
String returnValue = postMethod.getResponseBodyAsString();
System.out.println(returnValue);
九)基于java-httpclient程序,以SOAP1.1协议POST方式访问http://www.webxml.com.cn网站,查询一些公共的webservice服务【test05】
XML是双方在传递过程中采用的一种通用的数据格式,每种平台,语言,项目都支持
SOAP协议:表示双方之间通过XML的方式定义数据交换的格式,这种协议叫SOAP协议
参见<<SOAP请求_响应XML文档.txt>>
SOAP--简单对象访问协议--Simple Object Access Protocol
SOAP分为二大类:
SOAP1.1:安全性不高,适合普通webservice应用,例如:手机号码归属地,这二天我们用的,JDK1.6只支持该版本协议
SOAP1.2:安全性高,适合金融相关的webservice应用,例如:在线银行支付
SOAP1.1和SOAP1.2都是规定请求和响应的XML文档如何编写
JDK6只支持SOAP1.1协议
HTTP协议与SOAP协议的区别?
参见<<HTTP协议与SOAP协议的区别.JPG>>
SOAP协议/POST提交方式:
将如下内容复制到一个soap_request.xml文件中:
<?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>13312341234</mobileCode>
<userID></userID>
</getMobileCodeInfo>
</soap:Body>
</soap:Envelope>
测试代码如下:
public static void main(String[] args){
String url = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
HttpClient ie = new HttpClient();
PostMethod postMethod = new PostMethod(url);
postMethod.setRequestHeader("content-type","text/xml; charset=utf-8");
InputStream is = UseHttpClientWithPost.class.getClassLoader().getResourceAsStream("cn/itcast/test05/soap_request.xml");
postMethod.setRequestBody(is);
int code = ie.executeMethod(postMethod);
System.out.println(code==200?"成功":"失败");
String returnValue = postMethod.getResponseBodyAsString();
System.out.println(returnValue);
}