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/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <GetSpeech xmlns="http://xmlme.com/WebServices"> <Request>string</Request> </GetSpeech> </soap:Body> </soap:Envelope> soap1.2POST /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/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 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/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.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: %sn",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/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 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)) } 总结
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |