Golang中如何发送SOAP请求

Golang中如何发送SOAP请求 我需要发送SOAP请求并解析响应,但我完全不了解SOAP,而且在Go语言中没有专门处理SOAP的包,只有XML包。

<soapenv:Envelope 	xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header 	xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
</soap:Header>
<soapenv:Body>
	<acb:GetData 	xmlns:acb="GO-EXAMPLE/">
		<exampleReq>123456789</exampleReq>
	</acb:GetData>
 </soapenv:Body>
</soapenv:Envelope>

更多关于Golang中如何发送SOAP请求的实战教程也可以访问 https://www.itying.com/category-94-b0.html

3 回复

好的,谢谢

更多关于Golang中如何发送SOAP请求的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


https://github.com/hooklift/gowsdl 似乎是 GitHub 上 Go 语言中最流行的 SOAP 库。我自己还没有测试过。

在Go语言中发送SOAP请求主要依赖于标准库的net/httpencoding/xml包。由于没有专门的SOAP包,你需要手动构建SOAP信封并处理HTTP请求。以下是一个完整的示例,展示如何发送SOAP请求并解析响应。

首先,定义一个结构体来表示SOAP信封,使用XML标签映射到SOAP命名空间。然后,使用http.Post发送请求,并使用xml.Unmarshal解析响应。

package main

import (
    "bytes"
    "encoding/xml"
    "fmt"
    "io/ioutil"
    "net/http"
)

// 定义SOAP信封结构
type Envelope struct {
    XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"`
    Body    Body     `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
}

type Body struct {
    GetData GetData `xml:"GetData"`
}

type GetData struct {
    XMLName    xml.Name `xml:"GO-EXAMPLE/ GetData"`
    ExampleReq string   `xml:"exampleReq"`
}

// 定义响应结构
type ResponseEnvelope struct {
    XMLName xml.Name   `xml:"Envelope"`
    Body    ResponseBody `xml:"Body"`
}

type ResponseBody struct {
    GetDataResponse GetDataResponse `xml:"GetDataResponse"`
}

type GetDataResponse struct {
    Result string `xml:"result"`
}

func main() {
    // 构建SOAP请求数据
    requestData := Envelope{
        Body: Body{
            GetData: GetData{
                ExampleReq: "123456789",
            },
        },
    }

    // 将结构体序列化为XML
    xmlData, err := xml.MarshalIndent(requestData, "", "  ")
    if err != nil {
        fmt.Printf("Error marshaling XML: %v\n", err)
        return
    }

    // 添加XML头部声明
    soapRequest := []byte(xml.Header + string(xmlData))

    // 设置SOAP端点URL(替换为实际URL)
    url := "http://example.com/soap-endpoint"

    // 创建HTTP请求
    req, err := http.NewRequest("POST", url, bytes.NewBuffer(soapRequest))
    if err != nil {
        fmt.Printf("Error creating request: %v\n", err)
        return
    }

    // 设置必要的HTTP头
    req.Header.Set("Content-Type", "text/xml; charset=utf-8")
    req.Header.Set("SOAPAction", "GO-EXAMPLE/GetData") // 根据服务要求设置SOAPAction

    // 发送请求
    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        fmt.Printf("Error sending request: %v\n", err)
        return
    }
    defer resp.Body.Close()

    // 读取响应体
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Printf("Error reading response: %v\n", err)
        return
    }

    fmt.Printf("Response Status: %s\n", resp.Status)
    fmt.Printf("Response Body: %s\n", string(body))

    // 解析SOAP响应
    var response ResponseEnvelope
    err = xml.Unmarshal(body, &response)
    if err != nil {
        fmt.Printf("Error unmarshaling response: %v\n", err)
        return
    }

    // 输出解析结果
    fmt.Printf("Parsed Result: %s\n", response.Body.GetDataResponse.Result)
}

在这个示例中:

  • 定义了EnvelopeBodyGetData结构体,使用XML标签映射到SOAP命名空间(如xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope")。
  • 使用xml.MarshalIndent将结构体序列化为XML,并添加标准XML头部。
  • 通过http.NewRequest创建POST请求,设置Content-Typetext/xmlSOAPAction头(根据具体服务调整)。
  • 使用http.Client发送请求并读取响应。
  • 定义响应结构体ResponseEnvelope,使用xml.Unmarshal解析XML响应体。

注意:

  • 替换urlSOAPAction为实际服务的值。
  • 响应结构体需要根据实际SOAP响应XML结构调整字段和标签。
  • 如果服务需要认证,可以在请求头中添加Authorization或其他字段。

如果SOAP服务使用HTTPS或需要处理错误,可以扩展代码添加TLS配置或错误处理逻辑。

回到顶部