阅读连接: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>