如何使用gowsdl库使soap客户端调用服务支持soap协议?

时间:2019-05-06 17:55:22

标签: web-services go soap

我正在使用库https://github.com/hooklift/gowsdl创建一个Soap客户端来调用服务并从该服务接收响应返回。服务器是http://www.dneonline.com/calculator.asmx?wsdl

我在chrome上使用 Wizdler 扩展名来查看端点,肥皂操作和受支持的http方法。

enter image description here

请求XML

enter image description here

响应XML

enter image description here

/src/soap_client.go

package src

import (
    "encoding/xml"
    "github.com/hooklift/gowsdl/soap"
)

type AddRequest struct {
    XMLName xml.Name `xml:"http://www.dneonline.com/calculator.asmx Calculator.CalculatorSoap"`

    IntA int `xml:"intA"`
    IntB int `xml:"intB"`
}

type AddResponse struct {
    XMLName xml.Name `xml:"http://www.dneonline.com/calculator.asmx Calculator.CalculatorSoap"`

    AddResponse AddResult `xml:"AddResponse"`
}

type AddResult struct {
    AddResult int `xml:"AddResult"`
}

type ISoapClient interface {
    Add(request *AddRequest) (*AddResponse, error)
}

type SoapClient struct {
    client *soap.Client
}

func NewSoapClient(client *soap.Client) *SoapClient {
    return &SoapClient{
        client: client,
    }
}

func (sap *SoapClient) Add(request *AddRequest) (*AddResponse, error) {
    response := new(AddResponse)
    err := sap.client.Call("Add", request, response)
    if err != nil {
        return nil, err
    }

    return response, nil
}

main.go

package main

import (
    "awesomeProject/src"
    "fmt"
    "github.com/hooklift/gowsdl/soap"
)

func main() {
    client := soap.NewClient(
        "http://www.dneonline.com/calculator.asmx",
    )
    request := src.AddRequest{
        IntA: 1,
        IntB: 1,
    }
    service := src.NewSoapClient(client)
    res, err := service.Add(&request)

    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Print(res.AddResponse)
}

我想在此服务的Add端点中使用Calculator操作。所以我尝试了sap.client.Call("<name_of_action>", request, response),但没有用。怎么做对?谢谢

调用上面的代码时出错:

  

System.Web.Services.Protocols.SoapException:服务器无法识别   HTTP Header SOAPAction的值:Add。在   System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest()   在   System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage   讯息)   System.Web.Services.Protocols.SoapServerProtocol.Initialize()在   System.Web.Services.Protocols.ServerProtocol.SetContext(Type type,   HttpContext上下文,HttpRequest请求,HttpResponse响应)位于   System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type,   HttpContext上下文,HttpRequest请求,HttpResponse响应,   布尔和中止处理)

0 个答案:

没有答案