我生成了wsdl代码并将其作为包安装.但是我很难理解从中调用方法的语法.
当我检查包装时,这就是我想要的肥皂体:
type AccountUser struct { XMLname xml.name `xml:"http://exacttarget.com/wsdl/partnerAPI AccountUser"` *APIObject AccountUserID int32 `xml:"AccountUserID,omitempty"` UserID string `xml:"UserID,omitempty"` Password string `xml:"Password,omitempty"` name string `xml:"name,omitempty"` Email string `xml:"Email,omitempty"` MustChangePassword bool `xml:"MustChangePassword,omitempty"` ActiveFlag bool `xml:"ActiveFlag,omitempty"` ChallengePhrase string `xml:"ChallengePhrase,omitempty"` ChallengeAnswer string `xml:"ChallengeAnswer,omitempty"` UserPermissions []*UserAccess `xml:"UserPermissions,omitempty"` Delete int32 `xml:"Delete,omitempty"` LastSuccessfulLogin time.Time `xml:"LastSuccessfulLogin,omitempty"` IsAPIUser bool `xml:"IsAPIUser,omitempty"` NotificationEmailAddress string `xml:"NotificationEmailAddress,omitempty"` IsLocked bool `xml:"IsLocked,omitempty"` Unlock bool `xml:"Unlock,omitempty"` BusinessUnit int32 `xml:"BusinessUnit,omitempty"` DefaultBusinessUnit int32 `xml:"DefaultBusinessUnit,omitempty"` DefaultApplication string `xml:"DefaultApplication,omitempty"` Locale *Locale `xml:"Locale,omitempty"` TimeZone *TimeZone `xml:"TimeZone,omitempty"` DefaultBusinessUnitObject *BusinessUnit `xml:"DefaultBusinessUnitObject,omitempty"` AssociatedBusinessUnits struct { BusinessUnit []*BusinessUnit `xml:"BusinessUnit,omitempty"` } `xml:"AssociatedBusinessUnits,omitempty"` Roles struct { Role []*Role `xml:"Role,omitempty"` } `xml:"Roles,omitempty"` LanguageLocale *Locale `xml:"LanguageLocale,omitempty"` SsoIDentitIEs struct { SsoIDentity []*SsoIDentity `xml:"SsoIDentity,omitempty"` } `xml:"SsoIDentitIEs,omitempty"`}
调用SOAP的方法是:
func (s *SOAPClIEnt) Call(soapAction string,request,response interface{}) error { envelope := SOAPEnvelope{ //header: Soapheader{},} envelope.Body.Content = request buffer := new(bytes.Buffer) encoder := xml.NewEncoder(buffer) //encoder.Indent(" "," ") if err := encoder.Encode(envelope); err != nil { return err } if err := encoder.Flush(); err != nil { return err } log.Println(buffer.String()) req,err := http.NewRequest("POST",s.url,buffer) if err != nil { return err } if s.auth != nil { req.SetBasicAuth(s.auth.Login,s.auth.Password) } req.header.Add("Content-Type","text/xml; charset=\"utf-8\"") if soapAction != "" { req.header.Add("SOAPAction",soapAction) } req.header.Set("User-Agent","gowsdl/0.1") req.Close = true tr := &http.Transport{ TLSClIEntConfig: &tls.Config{ InsecureSkipVerify: s.tls,},Dial: dialTimeout,} clIEnt := &http.ClIEnt{Transport: tr} res,err := clIEnt.Do(req) if err != nil { return err } defer res.Body.Close() rawbody,err := IoUtil.ReadAll(res.Body) if err != nil { return err } if len(rawbody) == 0 { log.Println("empty response") return nil } log.Println(string(rawbody)) respEnvelope := new(SOAPEnvelope) respEnvelope.Body = SOAPBody{Content: response} err = xml.Unmarshal(rawbody,respEnvelope) if err != nil { return err } fault := respEnvelope.Body.Fault if fault != nil { return fault } return nil}
我已经将包导入到我的go文件中,并且喜欢如何调用它的指针.
要使用生成的代码,您显然必须首先使用生成的“构造函数”函数NewSOAPClIEnt或NewSOAPClIEntWithTLSConfig初始化soap客户端.之后,您需要准备两个可用作Call方法的请求和响应参数的值,它们表示soap请求/响应有效负载的正文内容.
这两个值的类型取决于您要进行的调用类型,例如假设调用create_account,update_account和delete_account通常需要不同的类型.基本上,请求值的类型应该可以编组到一个xml中,该xml与soap服务为指定 *** 作所期望的xml相匹配,并且响应的类型应该是从xml中可解组的,该xml与soap服务的指定响应相匹配行动.
考虑这个人为的例子:
有一个允许您创建用户的SOAP服务.为了能够使用该服务创建用户,它需要您发送电子邮件和密码,如果一切正常,它将返回一个ID.在这种情况下,您的两个请求/响应类型将如下所示:
type createuserRequest struct { Email string `xml:"Email,omitempty"` Password string `xml:"Password,omitempty"`}type createuserResponse struct { ID string `xml:"ID"`}
然后客户端代码看起来像这样:
clIEnt := NewSOAPClIEnt("https://soap.example.com/call",true,nil)req := &createuserRequest{ Email: "[email protected]",Password: "1234567890",}res := &createuserResponse{}if err := clIEnt.Call("create_user",req,res); err != nil { panic(err)}// if everything went well res.ID should have its// value set with the one returned by the service.fmt.Println(res.ID)总结
以上是内存溢出为你收集整理的用Golang调用SOAP全部内容,希望文章能够帮你解决用Golang调用SOAP所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)