SOAP--------Golang對接WebService服務實戰

背景

最近項目中在對接某保險公司線上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 main

import (
    "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))
}

總結

以需求驅動技術,技術本身沒有優略之分,只有業務之分。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章