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

背景

最近項(xiàng)目中在對接某保險公司線上webService接口時,無奈Golang沒有像java那般有現(xiàn)成的jar包或庫使用,只好從底層基于soap協(xié)議通過http post來實(shí)現(xiàn)對接。
對接過程中,由于開始并未注意版本問題(webService接口使用soap1.2協(xié)議版本,對接時使用soap1.1協(xié)議版本),導(dǎo)致很長時間對接報500返回。

成都創(chuàng)新互聯(lián)公司是一家專注于成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計與策劃設(shè)計,馬關(guān)網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:馬關(guān)等地區(qū)。馬關(guān)做網(wǎng)站價格咨詢:18980820575

soap 簡介

SOAP(Simple Object Access Protocol )簡單對象訪問協(xié)議是在分散或分布式的環(huán)境中交換信息的簡單的協(xié)議,是一個基于XML的協(xié)議,它包括四個部分:SOAP封裝(envelop),封裝定義了一個描述消息中的內(nèi)容是什么,是誰發(fā)送的,誰應(yīng)當(dāng)接受并處理它以及如何處理它們的框架;SOAP編碼規(guī)則(encoding rules),用于表示應(yīng)用程序需要使用的數(shù)據(jù)類型的實(shí)例; SOAP RPC表示(RPC representation),表示遠(yuǎn)程過程調(diào)用和應(yīng)答的協(xié)定;SOAP綁定(binding),使用底層協(xié)議交換信息。

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數(shù)據(jù),得出以下差異

兩者的命名空間不同。

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消息格式不同。

主要體現(xiàn)在消息格式的命名空間上。

golang 編碼對接服務(wù)(使用官方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))
}

總結(jié)

以需求驅(qū)動技術(shù),技術(shù)本身沒有優(yōu)略之分,只有業(yè)務(wù)之分。

網(wǎng)頁名稱:SOAP--------Golang對接WebService服務(wù)實(shí)戰(zhàn)
文章轉(zhuǎn)載:http://muchs.cn/article12/jpghgc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、網(wǎng)頁設(shè)計公司品牌網(wǎng)站制作、動態(tài)網(wǎng)站、面包屑導(dǎo)航網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站