访问公网WebService服务
接下来,我们演示如何访问公网webservice服务。
我们以访问 http://www.webxml.com.cn/zh_cn/index.aspx 为例,主要演示手机号码归属地查询服务(使用说明书路径为 http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL)。接着,生成相应的webservice客户端,以及jar包。
gen-c-s.bat(生成webservice服务的客户端):
wsimport -s E:\webService\1\source -d E:\webService\1\classes http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL
gen-jar.bat(生成项目依赖的jar包):
e: cd E:\webService\1\classes jar cvf phoneCode.jar cn
然后,创建一个web案例,添加生成的 phoneCode.jar包为项目依赖。
<%--前端页面--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>号码归属地查询</title>
<script language="JavaScript" src="jquery.min.1.11.3.js"></script>
<script language="JavaScript">
var num = 1;
function doQuery() {
var phoneCode = $('#phoneCode').val();
console.log(phoneCode);
if (phoneCode.length == 11) {
if (num == 1) {
num = 0;
console.log('发起请求归属地查询:' + phoneCode);
$.ajax({
url: '/QueryServlet',
type: 'POST', //GET
async: true, //或false,是否异步
data: {
phoneCode: phoneCode
},
timeout: 10000, //超时时间
dataType: 'json',
success: function (data, textStatus, jqXHR) {
num = 1;
console.log("success--->" + phoneCode + ":" + data.resultPhone);
$('#pResult').css({"color": "blue", "font-size": "16px", "font-weight": "bold"});
$('#pResult').text(phoneCode + ":" + data.resultPhone);
},
error: function (xhr, textStatus) {
num = 1;
console.log("error--->");
}
});
}
}
}
</script>
</head>
<body>
<form>
<input id="phoneCode" type="text" name="phoneCode" placeholder="请填写要充值电话号码" onkeyup="doQuery()">
<p id="pResult"></p>
</form>
</body>
</html>
-----------------------------------------------
//后端控制器
package com.itszt;
import cn.com.webxml.MobileCodeWS;
import cn.com.webxml.MobileCodeWSSoap;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 处理前端发来的请求,并给前端响应数据
*/
@WebServlet(name = "QueryServlet", urlPatterns = "/QueryServlet")
public class QueryServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//接收前端发来的参数
String phoneCode = request.getParameter("phoneCode");
//创建服务视图
MobileCodeWS mobileCodeWS = new MobileCodeWS();
//根据服务视图得到服务端点
MobileCodeWSSoap mobileCodeWSSoap = mobileCodeWS.getMobileCodeWSSoap();
//调用服务端点的方法
String mobileCodeInfo = mobileCodeWSSoap.getMobileCodeInfo(phoneCode, "");
System.out.println("mobileCodeInfo = " + mobileCodeInfo);
if (mobileCodeInfo != null) {
if (mobileCodeInfo.contains(":")) {
String[] split = mobileCodeInfo.split(":");
response.setCharacterEncoding("UTF-8");
response.getWriter().write("{\"resultPhone\":\"" + split[1] + "\"}");
} else {
System.out.println("解析失败");
}
} else {
System.out.println("解析失败");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
项目运行后,前端反馈如下:


浙公网安备 33010602011771号