Go   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了SOAP--------Golang对接WebService服务实战大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

背景

soap 简介

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/XMLscheR_594_11845@a-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLscheR_594_11845@a" 
  xmlns:soap="http://scheR_594_11845@as.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/XMLscheR_594_11845@a-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLscheR_594_11845@a" 
  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数据,得出以下差异

两者的命名空间不同。

http头信息上存在差异。

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/XMLscheR_594_11845@a-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLscheR_594_11845@a" 
  xmlns:soap="http://scheR_594_11845@as.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/XMLscheR_594_11845@a-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLscheR_594_11845@a" 
  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,"application/soap+xml; charset=utf-8",err)
        return
    }
    defer res.body.Close()

    // return status
    if http.StatusOk != res.StatusCode {
        fmt.Println("Webservice soap1.2 request fail,err)
        return
    }

    fmt.Println("webservice soap1.2 response: ",String(data))
}

大佬总结

以上是大佬教程为你收集整理的SOAP--------Golang对接WebService服务实战全部内容,希望文章能够帮你解决SOAP--------Golang对接WebService服务实战所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。