使用多种方式调用webservice服务
2014-11-07 16:32
211 查看
客户端调用webService有多种方式:
下面就以为http://www.webxml.com.cn/zh_cn/index.aspx上提供的, 查询手机号码为例, 阐述一下几种方式:
1: 使用wsimport, 或者是第三方框架的命令(比如cxf的wsdl2java)来自动生成代码
首先, 在命令行中运行:
下面就以为http://www.webxml.com.cn/zh_cn/index.aspx上提供的, 查询手机号码为例, 阐述一下几种方式:
1: 使用wsimport, 或者是第三方框架的命令(比如cxf的wsdl2java)来自动生成代码
首先, 在命令行中运行:
wsimport -s . http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl[/code]生成客户端代码, 并把生成的源码拷贝到项目中.
然后, 调用public class PhoneCodeApp { public static void main(String[] args) { MobileCodeWS server = new MobileCodeWS(); MobileCodeWSSoap soapPortType = server.getMobileCodeWSSoap(); ArrayOfString result = soapPortType.getDatabaseInfo(); for (String str : result.getString()) { System.out.println(str); } System.out.println(soapPortType.getMobileCodeInfo("15513061132", "")); } }
2: 利用jax-ws中的service类, 收到编写代码进行调用
首先, 也是需要命令来获得客户端代码, 但是仅仅需要soap调用接口.
然后编写代码:public class ServiceCode { public static void main(String[] args) throws Exception { URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl"); QName qName = new QName("http://WebXml.com.cn/", "MobileCodeWS"); Service service = Service.create(url, qName); MobileCodeWSSoap portType = service.getPort(MobileCodeWSSoap.class); ArrayOfString string = portType.getDatabaseInfo(); for (String str : string.getString()) { System.out.println(str); } System.out.println(portType.getMobileCodeInfo("15513061132", "")); } }3: 使用urlconnection手动组装soap请求协议来调用服务
这种方式与上面2种不同, 是通过组装请求协议来调用的,因此不需要运行命令来获得代码
只需要编写代码即可.public class PhoneCodeApp { public static void main(String[] args) throws Exception { // 建立连接 URL url = new URL( "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "text/xml;charset=utf-8"); connection.setRequestProperty("SOAPAction", "http://WebXml.com.cn/getDatabaseInfo"); // 发送数据 OutputStream outputStream = connection.getOutputStream(); String str = "<?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><getDatabaseInfo xmlns=\"http://WebXml.com.cn/\" /></soap:Body></soap:Envelope>"; outputStream.write(str.getBytes(Charset.forName("utf-8"))); connection.connect(); // 接受数据 InputStream inputStream = connection.getInputStream(); outputStream.close(); inputStream.close(); connection.disconnect(); // 解析文档 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(inputStream); NodeList nodeList = document.getElementsByTagName("string"); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); System.out.println(node.getTextContent()); } } }
4: 在页面上通过ajax来调用
这种方式本质同上面的方式一样,直接上代码<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript"> window.onload = function() { document.getElementById("btn").onclick = function() { var xmlHttpRequest = null; try { xmlHttpRequest = new XMLHttpRequest(); } catch (e) { try { xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } } var reqeustStr = '<?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><getDatabaseInfo xmlns=\"http://WebXml.com.cn/\" /></soap:Body></soap:Envelope>'; xmlHttpRequest.open("POST", "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx", true); xmlHttpRequest.setRequestHeader("Content-Type", "text/xml;charset=utf-8"); xmlHttpRequest.send(reqeustStr); xmlHttpRequest.onreadystatechange = function() { if(xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) { alert(xmlHttpRequest.responseText); } } } } </script> </head> <body> <input type="button" value="发送请求" id="btn"/> </body> </html>
相关文章推荐
- WebService服务的发布及多种客户端方式调用
- axis2客户端调用免费的webservice服务的实例之三axis2使用RPC方式调用网上免费WebService
- 【webservice】cxf多种调用webservice服务的方式
- 使用WebService的方式调用部署在服务器的Wcf服务
- webservice接口常见问题1:客户端使用SOAP方式调用CXF服务时异常
- 使用C#创建webservice及三种调用方式
- 使用C#创建webservice及三种调用方式
- 使用C#创建webservice及三种调用方式
- 使用Xire代理方式调用webservice方法
- 使用 SOAP 协议调用 Web服务(WebService)
- Silverlight调用WebService时出现的跨域方式访问服务
- 不使用VS.NET集成开发环境,调用微软的WebService注册服务
- 在BizTalk使用管道及映射的方式调用安全电子邮件系统发布的服务
- 使用C#创建webservice及三种调用方式
- 使用C#创建webservice及三种调用方式 (ASP.NETweb编程常用到的27个函数集)
- 使用C#创建webservice及调用方式
- 使用C#创建webservice及三种调用方式
- 完全使用接口方式调用WCF 服务
- 使用C#创建webservice及三种调用方式
- 使用C#创建webservice及三种调用方式