WebService 基础知识点和用Postman调试

  1. 云栖社区>
  2. 博客>
  3. 正文

WebService 基础知识点和用Postman调试

kandy_aliyq 2018-05-27 16:37:00 浏览768
展开阅读全文

阅读连接:Retrofit 用Soap协议访问WebService 详解

参考

1、java发HTTP POST请求(内容为xml格式)
2、 android解析XML总结(SAX、Pull、Dom三种方式)
3、Android利用Soap读取WebService并且解析XML的DataSet数据

前言

1、首先不要把这个想的太复杂,它就是使用【soap】协议的请求,数据格式都是【xml】,基础还是http的post请求,但是它的规范显然更多一些,总体逃不过【Request和Response】。
2、以下所有的范例都是使用 【 WeatherWebService 】 这个网站,它提供了【Soap1.1 和 Soap1.2 】的请求范例,有【Request和Response】报文可看,这样更好理解规范和格式
注意点:
1、Soap1.1 、Soap1.2 :不同版本协议,代表的header和xml都略有不同
2、Baseurl、Header(Content-type、SOAPAction)、RequestBody(Xml)、ResponseBody(Xml)
3、RequestBody(Xml):Envelope,NameSpace、Body、Method、Param
3、ResponseBody(Xml):Envelope,NameSpace、Body、Method、Param

WebService 基础与注意点

举例:天气网站-获得某省份下所有城市

Soap1.1:

1、xmlns后基本都是namespace,比如envelopse标签有三个namespace,getSupportCity这个方法名有一个namespace
2、区分soap1.1的是:【xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"】
3、soap1.1的请求header有:【Content-Type: text/xml; charset=utf-8 】和【SOAPAction: "http://WebXml.com.cn/getSupportCity"】

//-------------------------------------Request------------------------------------
POST /WebServices/WeatherWebService.asmx HTTP/1.1
Host: www.webxml.com.cn
Content-Type: text/xml; charset=utf-8   //header中的哦~~
Content-Length: length
SOAPAction: "http://WebXml.com.cn/getSupportCity"  //header中的哦~~

<?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/">  //标记为soap1.1协议
  <soap:Body>
    <getSupportCity xmlns="http://WebXml.com.cn/">  //method和其namespace
      <byProvinceName>string</byProvinceName>     //param
    </getSupportCity>
  </soap:Body>
</soap:Envelope>

//-------------------------------------Response------------------------------------
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?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>
    <getSupportCityResponse xmlns="http://WebXml.com.cn/">  //结果集啦~~
      <getSupportCityResult>
        <string>string</string>
        <string>string</string>
      </getSupportCityResult>
    </getSupportCityResponse>
  </soap:Body>
</soap:Envelope>
Soap1.2:

1、略,同上
2、区分soap1.2的是:【xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"】
3、soap1.2的请求header有:【application/soap+xml; charset=utf-8 】和没有【SOAPAction】

//-------------------------------------Requeset------------------------------------
POST /WebServices/WeatherWebService.asmx HTTP/1.1
Host: www.webxml.com.cn
Content-Type: application/soap+xml; charset=utf-8  //header中的,与soap1.1不同哦,而且没有soapaction了,需要注意~~~~
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> //标记为soap1.2协议
  <soap12:Body>
    <getSupportCity xmlns="http://WebXml.com.cn/">
      <byProvinceName>string</byProvinceName>
    </getSupportCity>
  </soap12:Body>
</soap12:Envelope>

//-------------------------------------Response------------------------------------
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <getSupportCityResponse xmlns="http://WebXml.com.cn/">  //结果集~~~
      <getSupportCityResult>
        <string>string</string>
        <string>string</string>
      </getSupportCityResult>
    </getSupportCityResponse>
  </soap12:Body>
</soap12:Envelope>

Postman调试(也可以用SoapUI)

soap1.1
img_cf2a19b5ceec864243374df0753f79a4.png
1.png
img_33f79d415aef804b796995658573a0f2.png
2.png
img_0ffa5780da0e898133aee964f296c19d.png
3.png
soap1.2
img_890cd0449f6ffaa2276fa4a14e761b56.png
5.png
img_9dee4267b1a93137d4a8d3fe91b17550.png
6.png

网友评论

登录后评论
0/500
评论
kandy_aliyq
+ 关注