转载:
web服务网址:
(2)调用公网手机号归属地查询服务
第一步:wsimport生成客户端代码
wsimport -p cn.itcast.mobile -s .
第二步:阅读使用说明书(wsdl文档),使用生成的客户端代码调用服务端
package cn.itcast.mobile.client;
import cn.itcast.mobile.MobileCodeWS;
import cn.itcast.mobile.MobileCodeWSSoap;
/**
*
* @ClassName: MobileClient
* @Description: TODO(公网手机号查询客户端)
* @author
* @date 2017年11月8日 上午8:35:02
*
*/
public class MobileClient {
public static void main(String[] args) {
MobileCodeWS mobileCodeWS = new MobileCodeWS();
MobileCodeWSSoap mobileCodeWSSoap = mobileCodeWS.getMobileCodeWSSoap();
String mobileCodeInfo = mobileCodeWSSoap.getMobileCodeInfo("18518114962", "");
System.out.println(mobileCodeInfo);
}
}
2. service编程调用方式
注意:
(1)该种方式可以自定义关键元素,方便以后维护,是一种标准的开发方式;
(2)这种方式同样需要wsimport生成客户端代码,只不过仅需要将服务接口类引入即可,例如如果需要<wsdl:port name="MobileCodeWSSoap" binding="tns:MobileCodeWSSoap">端口服务,则需要将生成的MobileCodeWSSoap.class类引入;
package cn.itcast.mobile.client;
import java.io.IOException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import cn.itcast.mobile.MobileCodeWSSoap;
/**
*
* Title: ServiceClient.java
* Description:Service编程实现服务端调用
* 这种方式同样需要wsimport生成客户端代码,只不过仅需要将服务接口类引入即可,例如如果需要
* <wsdl:port name="MobileCodeWSSoap" binding="tns:MobileCodeWSSoap">
* 端口服务,则需要将生成的MobileCodeWSSoap.class类引入
*/
public class ServiceClient {
public static void main(String[] args) throws IOException {
URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");
QName qname = new QName("http://WebXml.com.cn/", "MobileCodeWS");
Service service = Service.create(url, qname);
MobileCodeWSSoap mobileCodeWSSoap = service.getPort(MobileCodeWSSoap.class);
String result = mobileCodeWSSoap.getMobileCodeInfo("1866666666", "");
System.out.println(result);
}
}
3. HttpURLConnection调用方式
package cn.itcast.mobile.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
*
* @ClassName: HttpURLConectionMode
* @Description: TODO(通过HttpURLConnection发送http请求)
* sope协议,比较麻烦的是需要拼接xml格式的请求数据
* @author
* @date 2017年11月8日 上午9:18:24
*
*/
public class HttpURLConectionMode {
public static void main(String[] args) throws IOException {
URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
connection.setDoInput(true);
connection.setDoOutput(true);
String soapXML = getXML("152266316");
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes());
int responseCode = connection.getResponseCode();
if(200 == responseCode){
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
StringBuilder sb = null;
try {
is = connection.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
sb = new StringBuilder();
String temp = null;
while(null != (temp = br.readLine())){
sb.append(temp);
}
System.out.println(sb.toString());
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
br.close();
isr.close();
is.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调用方式(存在跨域问题)
跨域解决参考:
jsonp无法实现跨域,因为webservice服务的响应格式为xml格式;
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript">
function queryMobile(){
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);
}
}
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);
}
</script>
</head>
<body>
手机号查询:<input type="text" id="phoneNum"/> <input type="button" value="查询" onclick="javascript:queryMobile();"/>
</body>
</html>