通过xml形式请求webService

wsdl地址:http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl


 
//soap请求体
POST /WebServices/WeatherWS.asmx HTTP/1.1
Host: ws.webxml.com.cn
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://WebXml.com.cn/getWeather"

<?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>
    <getWeather xmlns="http://WebXml.com.cn/">
      <theCityCode>string</theCityCode>
      <theUserID>string</theUserID>
    </getWeather>
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

//soap响应体
<?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>
    <getWeatherResponse xmlns="http://WebXml.com.cn/">
      <getWeatherResult>
        <string>string</string>
        <string>string</string>
      </getWeatherResult>
    </getWeatherResponse>
  </soap:Body>
</soap:Envelope>

创建jiava项目

java类

public class Demo { /**       * 访问服务       * @param wsdl wsdl地址       * @param ns 命名空间       * @param method 方法名       * @param params 参数       * @return       * @throws Exception       */   public static void main(String[] args) {     //String wsdl,String ns,String method,Map<String,String> params,String result     String wsdl="http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl";//wsdl地址          int time = 30000;//设置请求超时时间                  HttpClient client = new HttpClient();         PostMethod postMethod = new PostMethod(wsdl);         //设置请求连接超时时间         client.getHttpConnectionManager().getParams().setConnectionTimeout(time*3);         //设置读取时间超时         client.getHttpConnectionManager().getParams().setSoTimeout(3*time);         //拼接SOAP           StringBuffer soapRequestData = new StringBuffer("");           soapRequestData.append("<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/\">");         soapRequestData.append("<soap:Body>");         soapRequestData.append(" <getWeather xmlns=\"http://WebXml.com.cn/\">");         soapRequestData.append("<theCityCode>宁波</theCityCode>");         soapRequestData.append("<theUserID></theUserID>");         soapRequestData.append(" </getWeather>");         soapRequestData.append(" </soap:Body>");         soapRequestData.append("</soap:Envelope>");          try { // 然后把Soap请求数据添加到PostMethod中   RequestEntity requestEntity = new StringRequestEntity(soapRequestData.toString(), "text/xml", "UTF-8"); postMethod.setRequestEntity(requestEntity); System.out.println(soapRequestData.toString()); int status =  client.executeMethod(postMethod); System.out.println("status:"+status);  //获得响应体输入流      InputStream response= postMethod.getResponseBodyAsStream();      String resp = IOUtils.toString(response,"UTF-8");      System.out.println(resp); } catch (Exception e) { e.printStackTrace(); }     }            }

响应 结果
<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> <getWeather xmlns="http://WebXml.com.cn/"><theCityCode>宁波</theCityCode><theUserID></theUserID> </getWeather> </soap:Body></soap:Envelope>
status:200
<?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><getWeatherResponse xmlns="http://WebXml.com.cn/"><getWeatherResult><string>浙江 宁波</string><string>宁波</string><string>2106</string><string>2016/11/13 15:36:11</string><string>今日天气实况:气温:22℃;风向/风力:东南风 2级;湿度:81%</string><string>紫外线强度:最弱。空气质量:良。</string><string>紫外线指数:最弱,辐射弱,涂擦SPF8-12防晒护肤品。
感冒指数:少发,无明显降温,感冒机率较低。
穿衣指数:较舒适,建议穿薄外套或牛仔裤等服装。
洗车指数:不宜,有雨,雨水和泥水会弄脏爱车。
运动指数:较不宜,有降水,推荐您在室内进行休闲运动。
空气污染指数:良,气象条件有利于空气污染物扩散。
</string><string>11月13日 小雨转阴</string><string>16℃/21℃</string><string>东南风转南风微风</string><string>7.gif</string><string>2.gif</string><string>11月14日 小雨</string><string>15℃/21℃</string><string>北风微风转东北风3-4级</string><string>7.gif</string><string>7.gif</string><string>11月15日 小雨转阴</string><string>13℃/18℃</string><string>东北风3-4级</string><string>7.gif</string><string>2.gif</string><string>11月16日 多云转阴</string><string>15℃/20℃</string><string>东北风转东风微风</string><string>1.gif</string><string>2.gif</string><string>11月17日 多云</string><string>15℃/21℃</string><string>东风转东南风微风</string><string>1.gif</string><string>1.gif</string></getWeatherResult></getWeatherResponse></soap:Body></soap:Envelope>
 需要的架包 

commons-codec-1.9.jar
commons-httpclient-3.1.jar
commons-io-2.2.jar
commons-logging-1.2.jar


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
利用wsdl.exe生成webservice代理类: 根据提供的wsdl生成webservice代理类 1、开始->程序->Visual Studio 2005 命令提示 2、输入如下红色标记部分 D:\Program Files\Microsoft Visual Studio 8\VC>wsdl /language:c# /n:TestDemo /out:d:\Temp\TestService.cs D:\Temp\TestService.wsdl 在d:/Temp下就会产生一个TestService.cs 文件 注意:D:\Temp\TestService.wsdlwsdl路径,可以是url路径:http://localhost/Temp/Test.asmx?wsdl wsdl参数说明: wsdl.exe ... - 选项 - - 指向 WSDL 协定、XSD 架构或 .discomap 文档的 URL 或路径。 /nologo 取消显示版权标志。 /language: 用于生成的代理类的语言。请从“CS”、“VB”、“JS”、“VJS”、 “CPP”中选择,或者为实现 System.CodeDom.Compiler.CodeDomProvider 的类提供一个完全限定的名称。默认语言为“CS”(CSharp)。 缩写形式为“/l:”。 /sharetypes 打开类型共享功能。此功能针对不同服务之间共享 的相同类型(命名空间、名称和网络签名必须相同) 创建一个具有单一类型定义的代码文件。 请使用 http:// URLs 作为命令行参数来引用 服务,或为本地文件创建一个 discomap 文档。 /verbose 指定 /sharetypes 开关时显示额外信息。 缩写形式为“/v”。 /fields 生成字段而非属性。缩写形式为“/f”。 /order 为粒子成员生成显式顺序标识符。 /enableDataBinding 在所有生成的类型上实现 INotifyPropertyChanged 接口, 以启用数据绑定。缩写形式为“/edb”。 /namespace: 生成的代理或模板的命名空间。默认命名空间 为全局命名空间。缩写形式为“/n:”。 /out: 生成的代理代码的文件名或目录路径。默认文件名是从 服务名派生的。缩写形式为“/o:”。 /protocol: 重写要实现的默认协议。请从“SOAP”、“SOAP12”、 “HttpGet”、“HttpPost”中选择。 /username: /password: /domain: 连接到要求身份验证的服务器时使用的凭据。 缩写形式为“/u:”、“/p:”和“/d:”。 /proxy: 用来处理 HTTP 请求的代理服务器的 URL。 默认为使用系统代理服务器设置。 /proxyusername: /proxypassword: /proxydomain: 连接到要求身份验证的代理服务器时使用的凭据。 缩写形式为“/pu:”、“/pp:”和“/pd:”。 /appsettingurlkey: 在代码生成中用来读取 URL 属性的 默认值的配置项。默认为不从配置 文件中读取。缩写形式为“/urlkey:”。 /appsettingbaseurl: 计算 URL 段时使用的基 URL。 还必须指定 appsettingurlkey 选项。URL 段是 从 appsettingbaseurl 计算 WSDL 文档中的 URL 的相对 URL 的结果。缩写形式为“/baseurl:”。 /parsableerrors 输出错误,其格式与编译器报告的格式类似。 - 高级 - /server 服务器开关已被否决。请改用 /serverInterface。 使用基于协定的 ASP.NET,为 Xml Web Services 实现 生成抽象类。默认情况下,生成客户端

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值