第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

Golang對(duì)接WebService服務(wù)實(shí)戰(zhàn)

標(biāo)簽:
Go

背景

最近项目中在对接某保险公司线上webService接口时,无奈Golang没有像java那般有现成的jar包或库使用,只好从底层基于soap协议通过http post来实现对接。
对接过程中,由于开始并未注意版本问题(webService接口使用soap1.2协议版本,对接时使用soap1.1协议版本),导致很长时间对接报500返回。

soap 简介

SOAP(Simple Object Access Protocol )简单对象访问协议是在分散或分布式的环境中交换信息的简单的协议,是一个基于XML的协议,它包括四个部分:SOAP封装(envelop),封装定义了一个描述消息中的内容是什么,是谁发送的,谁应当接受并处理它以及如何处理它们的框架;SOAP编码规则(encoding rules),用于表示应用程序需要使用的数据类型的实例; SOAP RPC表示(RPC representation),表示远程过程调用和应答的协定;SOAP绑定(binding),使用底层协议交换信息。

soap 版本

soap1.1请求

POST /WSShakespeare.asmx HTTP/1.1 Host: www.xmlme.com Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "http://xmlme.com/WebServices/GetSpeech"<?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>     <GetSpeech xmlns="http://xmlme.com/WebServices">       <Request>string</Request>     </GetSpeech>   </soap:Body> </soap:Envelope>

soap1.2

POST /WSShakespeare.asmx HTTP/1.1 Host: www.xmlme.com 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>     <GetSpeech xmlns="http://xmlme.com/WebServices">       <Request>string</Request>     </GetSpeech>   </soap12:Body> </soap12:Envelope>

通过对比1.1和1.2请求head和body数据,得出以下差异

两者的命名空间不同。

soap 1.1 使用 xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
soap 1.2 使用 xmlns:soap="http://www.w3.org/2003/05/soap-envelope"

HTTP头信息上存在差异。

soap 1.1 使用 为Content-Type: text/xml; charset=UTF-8
soap 1.1 使用 SOAPAction
soap 1.2 使用 为Content-Type: application/soap+xml;charset=UTF-8
soap 1.2 不使用SOAPAction

SOAP消息格式不同。

主要体现在消息格式的命名空间上。

golang 编码对接服务(使用官方net/http包)

package mainimport (    "net/http"    "strings"    "io/ioutil"    "fmt")func main() {    Soap11("https://lisea.cn/test")    Soap12("https://lisea.cn/test")}func Soap11(url string) {    reqBody := `<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>     <GetSpeech xmlns="http://xmlme.com/WebServices">       <Request>string</Request>     </GetSpeech>   </soap:Body> </soap:Envelope>`    res, err := http.Post(url, "text/xml; charset=UTF-8", strings.NewReader(reqBody))    if nil != err {        fmt.Println("http post err:", err)        return    }    defer res.Body.Close()    // return status    if http.StatusOk != res.StatusCode {        fmt.Println("WebService soap1.1 request fail, status: %s\n", res.StatusCode)        return    }    data, err := ioutil.ReadAll(res.Body)    if nil != err {        fmt.Println("ioutil ReadAll err:", err)        return    }    fmt.Println("webService soap1.1 response: ", string(data))}// soap1.2例子func Soap12(url string) {    reqBody := `<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>     <GetSpeech xmlns="http://xmlme.com/WebServices">       <Request>string</Request>     </GetSpeech>   </soap12:Body> </soap12:Envelope>`    res, err := http.Post(url, "application/soap+xml; charset=utf-8", strings.NewReader(reqBody))    if nil != err {        fmt.Println("http post err:", err)        return    }    defer res.Body.Close()    // return status    if http.StatusOk != res.StatusCode {        fmt.Println("WebService soap1.2 request fail, status: %s\n", res.StatusCode)        return    }    data, err := ioutil.ReadAll(res.Body)    if nil != err {        fmt.Println("ioutil ReadAll err:", err)        return    }    fmt.Println("webService soap1.2 response: ", string(data))}

总结

以需求驱动技术,技术本身没有优略之分,只有业务之分。

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
移動(dòng)開發(fā)工程師
手記
粉絲
39
獲贊與收藏
245

關(guān)注作者,訂閱最新文章

閱讀免費(fèi)教程

  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消