webService调用 查询手机号码和天气
发布时间:2020-12-17 01:16:20 所属栏目:安全 来源:网络整理
导读:? 1.? 下载axis 2.? 建立web? project 工程 3.??? 创建check.jsp ? %@ page language="java" import="java.util.*" pageEncoding="GB18030"%!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"html body bgcolor="6699FF" center h3欢迎来到手
?1.? 下载axis 2.? 建立web? project 工程 3.??? 创建check.jsp ? <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body bgcolor="6699FF">
<center>
<h3>欢迎来到手机查询,天气查询系统 </h3>
<form action="checkConfig.jsp" method="post">
<table width="400" height="100" border="1" bgcolor="">
<tr>
<td>
<select name="select">
<option value="number">输入查询的号码</option>
<option value="local">输入查询的天气地区</option>
</select>
</td>
<td>
<input type="text" name="value"/>
</td>
</tr>
<tr>
<td>
<input type="submit" value="查询"/>
</td>
<td>
<input type="reset" value ="重置"/>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
5. 创建checkConfig.jsp
? <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <%@page import="com.tools.TelephoneAxis2"%> <%@page import="com.tools.WeatherAxis2"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <body> <% request.setCharacterEncoding("GB18030"); String select = request.getParameter("select").trim(); String value = request.getParameter("value").trim(); System.out.println(value); String result = null; if(select.equals("number")) result = new TelephoneAxis2(value).getResult(); else result = new WeatherAxis2(value).getResult(); %> <center> <%=result%> </center> </body> </html> 6. 建立WeatherAxis2.java
? package com.tools; import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMNamespace; import org.apache.axiom.soap.SOAP11Constants; import org.apache.axis2.Constants; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; import org.apache.axis2.transport.http.HttpTransportProperties.ProxyProperties; public class WeatherAxis2 { private String local; public WeatherAxis2(String local){ this.local = local; } private static EndpointReference targetEPR = new EndpointReference("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"); public String getResult() throws Exception { ServiceClient sender = new ServiceClient(); sender.setOptions(buildOptions()); OMElement result = sender.sendReceive(buildParam(local)); return result.toString(); } private static OMElement buildParam(String local) { OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace("http://WebXml.com.cn/",""); OMElement data = fac.createOMElement("getWeatherbyCityName",omNs); OMElement inner = fac.createOMElement("theCityName",omNs); // inner.setText("吉首"); inner.setText(local); data.addChild(inner); return data; } private static Options buildOptions() { Options options = new Options(); options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI); options.setAction("http://WebXml.com.cn/getWeatherbyCityName"); options.setTo(targetEPR); //options.setProperty 如果不是通过代理上网,此句可省 //options.setProperty(HTTPConstants.PROXY,buildProxy()); options.setTransportInProtocol(Constants.TRANSPORT_HTTP); return options; } /** * 本机采用代理服务器上网时,需要设置代理 * @return */ public static ProxyProperties buildProxy() { ProxyProperties proxyProperties = new ProxyProperties(); proxyProperties.setProxyName("代理名称"); proxyProperties.setProxyPort(8080); return proxyProperties; } } 7. 建立TelephoneAxis2.java package com.tools; import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMNamespace; import org.apache.axiom.soap.SOAP11Constants; import org.apache.axis2.Constants; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; public class TelephoneAxis2 { String number; public TelephoneAxis2(String number) { this.number = number; } private static EndpointReference targetEPR = new EndpointReference("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"); public String getResult() throws Exception { String s = null; ServiceClient sender = new ServiceClient(); sender.setOptions(buildOptions()); OMElement result = sender.sendReceive(buildParam(number)); s = result.getFirstElement().getText(); return s; } private static OMElement buildParam(String number) { OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace("http://WebXml.com.cn/",""); OMElement data = fac.createOMElement("getMobileCodeInfo",omNs); OMElement inner = fac.createOMElement("mobileCode",omNs); // inner.setText("15874333357"); inner.setText(number); data.addChild(inner); return data; } private static Options buildOptions() { Options options = new Options(); options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI); options.setAction("http://WebXml.com.cn/getMobileCodeInfo"); options.setTo(targetEPR); //options.setProperty 如果不是通过代理上网,此句可省 //options.setProperty(HTTPConstants.PROXY,buildProxy()); options.setTransportInProtocol(Constants.TRANSPORT_HTTP); return options; } /** * 本机采用代理服务器上网时,需要设置代理 * @return public static ProxyProperties buildProxy() { ProxyProperties proxyProperties = new ProxyProperties(); proxyProperties.setProxyName("代理名称"); proxyProperties.setProxyPort(8080); return proxyProperties; } */ }8. 测试数据,完成 。只是没有图片而已。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |