分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

调用已发布的WebService

发布时间:2023-09-06 01:12责任编辑:熊小新关键词:Web

WebService服务演示

  1.          登录http://www.webxml.com.cn
  2.           单击手机查询服务

  3.         选择要调用的方法 例如: getMobileCodeInfo.

  4.  输入要查询的手机号单击”调用” 截图如下, 免费用户 UserID为null

a)   可以看到返回如下结果:

<?xml version="1.0" encoding="utf-8" ?> ??<string xmlns="http://WebXml.com.cn/">18323455678:重庆 重庆 重庆移动全球通卡</string>

  每种访问方式都有对应的说明,方法、参数与对应的返回数据。在点击方法名之后可以查看。一般四种。soap1.1,soap1.2,get,post。一般访问方式  HttpClient

Jar包:

1.Java方式访问WebService

(1)get方式:

说明:

url:  http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo ?后面拼接参数

Java代码:

package ws_a;import java.io.ByteArrayOutputStream;import java.io.FileInputStream;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.methods.PostMethod;/**调用第三方的webservice服务 ,获取电话号码信息 * ?*/public class MobileCodeService { ???//1. http-get方式访问webservice ???public void get(String mobileCode ,String userID ) throws Exception{ ???????URL url=new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode="+mobileCode+ ???????????????"&userID="+userID); ???????HttpURLConnection conn=(HttpURLConnection) url.openConnection(); ???????conn.setConnectTimeout(5000); ???????conn.setRequestMethod("GET"); ???????if(conn.getResponseCode()==HttpURLConnection.HTTP_OK){ //结果码=200 ???????????InputStream is=conn.getInputStream(); ???????????//内存流 , ?????????????ByteArrayOutputStream boas=new ByteArrayOutputStream(); ???????????byte[] buffer=new byte[1024]; ???????????int len=-1; ???????????while((len=is.read(buffer))!=-1){ ???????????????boas.write(buffer, 0, len); ???????????} ???????????System.out.println("GET请求获取的数据:"+boas.toString()); ???????????????boas.close(); ???????????is.close(); ???????} ???} ???????public static void main(String[] args) throws Exception{ ???????MobileCodeService ws=new MobileCodeService(); ???????ws.get("15110410513", ""); ???}}

结果;

(2)post方式

说明:

java代码:

package ws_a;import java.io.ByteArrayOutputStream;import java.io.FileInputStream;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.methods.PostMethod;/**调用第三方的webservice服务 ,获取电话号码信息 * ?*/public class MobileCodeService { ???????//2.Post请求 :通过Http-Client 框架来模拟实现 Http请求 ???public void post(String mobileCode ,String userID) throws Exception{ ???????/**HttpClient访问网络的实现步骤: ????????* ?1. 准备一个请求客户端:浏览器 ?????????* ?2. 准备请求方式: GET 、POST ????????* ?3. 设置要传递的参数 ????????* ?4.执行请求 ????????* ?5. 获取结果 ????????*/ ???????HttpClient client=new HttpClient(); ???????PostMethod postMethod=new PostMethod("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo"); ???????//3.设置请求参数 ???????postMethod.setParameter("mobileCode", mobileCode); ???????postMethod.setParameter("userID", userID); ???????//4.执行请求 ,结果码 ???????int code=client.executeMethod(postMethod); ???????//5. 获取结果 ???????String result=postMethod.getResponseBodyAsString(); ???????System.out.println("Post请求的结果:"+result); ???} ???public static void main(String[] args) throws Exception{ ???????MobileCodeService ws=new MobileCodeService(); ???????ws.post("13626217879", ""); ???}}

结果:

 (3)soap1.1访问

说明:

java代码:

package ws_a;import java.io.ByteArrayOutputStream;import java.io.FileInputStream;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.methods.PostMethod;/**调用第三方的webservice服务 ,获取电话号码信息 * ?*/public class MobileCodeService { ???//2.Post请求 :通过Http-Client 框架来模拟实现 Http请求 ???public void soap() throws Exception{ ???????HttpClient client=new HttpClient(); ???????PostMethod postMethod=new PostMethod("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx"); ???????//3.设置请求参数 ?????????postMethod.setRequestBody(new FileInputStream("C:\\Users\\liqiang\\Desktop\\soap.xml")); ??????????//修改请求的头部 ?????????postMethod.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); ???????//4.执行请求 ,结果码 ???????int code=client.executeMethod(postMethod); ???????System.out.println("结果码:"+code); ???????//5. 获取结果 ???????String result=postMethod.getResponseBodyAsString(); ???????System.out.println("Post请求的结果:"+result); ???} ???public static void main(String[] args) throws Exception{ ???????MobileCodeService ws=new MobileCodeService(); ???????ws.soap(); ???}}

桌面的soap.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>13834786998</mobileCode> ?????<userID></userID> ???</getMobileCodeInfo> ?</soap:Body></soap:Envelope>

结果:

结果码:200Post请求的结果:<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/"><getMobileCodeInfoResult>13834786998:山西 长治 山西移动全球通卡</getMobileCodeInfoResult></getMobileCodeInfoResponse></soap:Body></soap:Envelope>

     问题:1. 如何解析结果
              2. 如何传递对象参数

调用已发布的WebService

原文地址:http://www.cnblogs.com/qlqwjy/p/7554647.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved