如何在 Go 中进行 SOAP 调用? [关闭]

2024-01-02

鉴于 Adwords 是 Google 的产品,而 Go 是 Google 的产品,那么多久才会出现用 Go 编写的 Adwords API 版本?

与这个问题相关的是另一个问题:是否有适用于 Go 的 SOAP 库?


我无法回答有关 adwords API 的问题,因为我没有看到该公司的公告,并且很难从外部预测发布前的时间。

那么我来回答你的第二个问题。

我不知道 Go 中有任何 SOAP 库(go-lang.cat-v.org http://go-lang.cat-v.org/似乎没有引用一个),但与大多数语言一样,处理简单 SOAP 消息的一种方法是使用基本的http http://golang.org/pkg/net/http/ and xml http://golang.org/pkg/encoding/xml/图书馆。

两个重要的操作是

1) 通过执行 POST 查询来获取答案:

resp, err := httpClient.Post(query, "text/xml; charset=utf-8", someXMLasBytes)

2)使用解码它xml.NewDecoder http://golang.org/pkg/encoding/xml/#NewDecoder进入所需的结构:

parser := xml.NewDecoder(bytes.NewBufferString(in)) 
err = parser.DecodeElement(&envelope, nil)

以下是用 Go 完成的 SOAP 查询的完整且带注释的示例(由此简化 https://github.com/Canop/Chrall/blob/master/go/src/chrallserver/mhsoapclient.go) :

package main   

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

// The URL of the SOAP server
const MH_SOAP_URL = "http://sp.mountyhall.com/SP_WebService.php"

// this is just the message I'll send for interrogation, with placeholders
//  for my parameters
const SOAP_VUE_QUERY_FORMAT = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:tns=\"urn:SP_WebService\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" ><SOAP-ENV:Body><mns:Vue xmlns:mns=\"uri:mhSp\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><numero xsi:type=\"xsd:string\">%d</numero><mdp xsi:type=\"xsd:string\">%s</mdp></mns:Vue></SOAP-ENV:Body></SOAP-ENV:Envelope>"

// Here I define Go structures, almost identical to the structure of the
// XML message we'll fetch
// Note that annotations (the string "return>item") allow to have a slightly
//  different structure or different namings

type SoapItem struct {
    Numero    int
    Nom       string
    Type      string
    PositionX int
    PositionY int
    PositionN int
    Monde     int
}
type SoapVue struct {
    Items []SoapItem "return>item" 
}
type SoapFault struct {
    Faultstring string
    Detail      string
}
type SoapBody struct {
    Fault          SoapFault
    ProfilResponse SoapProfil
    VueResponse    SoapVue
}
type SoapEnvelope struct {
    XMLName xml.Name
    Body    SoapBody
}

// Here is the function querying the SOAP server
// It returns the whole answer as a Go structure (a SoapEnvelope)
// You could also return an error in a second returned parameter
func GetSoapEnvelope(query string, numero int, mdp string) (envelope *SoapEnvelope) {
    soapRequestContent := fmt.Sprintf(query, numero, mdp)
    httpClient := new(http.Client)
    resp, err := httpClient.Post(MH_SOAP_URL, "text/xml; charset=utf-8", bytes.NewBufferString(soapRequestContent)) 
    if err != nil {
        // handle error
    }
    b, e := ioutil.ReadAll(resp.Body) // probably not efficient, done because the stream isn't always a pure XML stream and I have to fix things (not shown here)
    if e != nil {
        // handle error
    }
    in := string(b)
    parser := xml.NewDecoder(bytes.NewBufferString(in))
    envelope = new(SoapEnvelope) // this allocates the structure in which we'll decode the XML
    err = parser.DecodeElement(&envelope, nil)
    if err != nil {
        // handle error
    }
    resp.Body.Close()
    return
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Go 中进行 SOAP 调用? [关闭] 的相关文章

  • 单值上下文中的多值错误

    我在编译 GO 代码时遇到此错误 multiple value fmt Println in single value context 我正在尝试创建一个函数 该函数接受可变数量的整数并将每个变量打印在一行上 GO package main
  • GoLang 中的 HTML 部分

    我刚刚开始使用 Go 我想用它创建一个网络应用程序 我现在尝试的是以handlebarsjs 式的方式使用模板 我想将页眉和页脚从主页中取出 以便可以将它们注入到每个网页上 我当前的设置应该是解析主页 页眉和页脚 HTML 文件并缓存它们
  • 当使用 k8s.io/client-go 库的 kubernetes 部署发生更改时获得通知的最佳方式是什么?

    Context 我正在编写一个使用k8s io client go https github com kubernetes client go 图书馆 这里是 godocs https godoc org k8s io client go
  • 关闭长度未知的通道

    当不了解频道时我无法关闭频道 length package main import fmt time func gen ch chan int var i int for time Sleep time Millisecond 10 ch
  • json.Unmarshal json字符串到对象是空结果[重复]

    这个问题在这里已经有答案了 我有一个非常简单的程序 如下所示 package main import encoding json fmt type RunCommand struct level string json level call
  • 如何在 PHPUnit 中跨多个测试模拟测试 Web 服务?

    我正在尝试使用 PHPUnit 测试 Web 服务接口类 基本上 这个类调用肥皂客户端目的 我正在尝试使用 PHPUnit 测试此类getMockFromWsdl这里描述的方法 http www phpunit de manual curr
  • “http:多个response.WriteHeader调用”有什么不好的影响?

    尽管我发现 http 多个响应 WriteHeader 调用 例外 但我的服务器表现良好 此异常不会导致我的服务器出现恐慌或行为异常 我进行了很多搜索 但只找到了如何解决这个问题 没有文档描述异常的不良影响 有人可以帮我找出为什么 http
  • go json marshal 的默认大小写选项?

    我有以下结构要导出为 json type ExportedIncident struct Title string json title Host string json host Status string json status Dat
  • 如何对结构切片而不是切片结构进行范围调整

    稍微玩了一下 Go HTML 模板后 我发现的所有循环模板中对象的示例都是将切片结构传递给模板 有点像这个示例 type UserList struct Id int Name string var templates template M
  • 从 Golang 调用 C 函数

    我想在 Golang 中编写控制器逻辑并处理 json 和数据库 同时在 C 中使用我的数学处理模型 在我看来 调用 C 函数的开销必须尽可能低 就像设置寄存器 rcx rdx rsi rdi 一样 执行一些操作fastcall 并获取 r
  • 所有可能的 GOOS 价值?

    如果我做对了 GOOS在编译源代码时确定 为了更好地支持多个操作系统 我感兴趣的是GOOS可能 当然 Go 是开源的 所以它可能有无限的可能性 所以我真正想要的是一个 通用列表 已知值为 windows linux darwin or fr
  • 如何在 C# 中向肥皂信封添加命名空间

    我想向我的肥皂信封添加命名空间设置 我想在 IClientMessageInspector 的 BeforeSendRequest 中更改它 或者您有更优雅的方法 例如
  • 如何在 Go 中使用与包同名的变量名?

    文件或目录的常见变量名称是 path 不幸的是 这也是 Go 中包的名称 此外 在 DoIt 中更改路径作为参数名称 如何编译此代码 package main import path os func main DoIt file txt f
  • Golang:如何在HTTP客户端的TLS配置中指定证书

    我有一个证书文件 该位置是 usr abc my crt我想将该证书用于我的 tls 配置 以便我的 http 客户端在与其他服务器通信时使用该证书 我当前的代码如下 mTLSConfig tls Config CipherSuites u
  • 仅导出嵌入结构实现的方法子集

    是否可以仅导出嵌入结构实现的方法的子集 这是一种与减少代码复制和粘贴非常不同的方法吗 还有更惯用的方法吗 type A struct func a A Hello fmt Println Hello func a A World fmt P
  • pq:函数unnest(未知)不是唯一的

    以下代码工作正常 但我想将 array a b c d e 定义为变量 rows err db Query select colname from SELECT date unnest array a b c d e AS colname
  • “反序列化操作回复消息正文时出错...” - 对于我调用的每个方法

    我正在尝试为我们的波兰拍卖服务 Allegro 创建非常简单的客户端应用程序 他们提供 SOAP 架构中的 API 问题是 每次我尝试调用任何方法时 我都会收到 反序列化操作 方法名称 的回复消息正文时出错 一般来说 我对网络服务不熟悉 所
  • 在 Golang 中生成固定长度的随机十六进制字符串的有效方法?

    我需要生成很多固定长度的随机十六进制字符串 我找到这个解决方案golang中如何生成固定长度的随机字符串 https stackoverflow com a 31832326 710955 我正在做这样的事情 const letterByt
  • 肥皂服务的良好框架是什么?

    我正在寻找一个用于肥皂的好框架service 我更喜欢使用Pythonic框架 但是在查看了soaplib rpclib 太不稳定 SOAPy 不适用于2 7 和ZSI 太 令人困惑 之后 我不确定这是否可能 我对使用另一种语言感到满意 尽
  • 如何访问主包之外的标志?

    We 解析标志 http golang org pkg flag FlagSet Parse当然 在 main 包中的 main go 中 然后我们有另一个包 我们想在其中读取一些标志的值 flags Args http golang or

随机推荐