由于自己搭建了java webservice,已经存在的wsdl.使用go去调用;获取其中的数据信息
,如果go搭建了webservice ,应该一样(个人理解)
package main
import (
"fmt"
"time"
// 缺包,自己补全
)
func main() {
// 参数封装
pars := make(map[string]interface{})
pars["Message"] = "this is test str" // 这是测试
pars["signMessage"] = ""
data, _ := json.Marshal(pars)
dataStr := string(data)
dataStr = Htmlquote(dataStr) // 网络中的 json文字
// fmt.Println("verify:" + dataStr)
//===============
postStr := CreateSOAPXml("http://itrus.com/itrusUtil", "verify", dataStr) // verify 为参数 http://itrus.com/itrusUtil为参数,参数内容根据实际情况获取
fmt.Println(postStr)
output := PostWebService("http://192.168.1.221:9080/itrus/services/itrusUtil", "http://itrus.com/itrusUtil", postStr) // http://192.168.1.221:9080/itrus/services/itrusUtil 为wsdl路径 postStr 为参数
fmt.Println("------", output)
// =======处理返回的xml 结果,获取其中想要的数据=====
result := "" // 获取返回的xml文件中的内容
var t xml.Token
var err error
outputReader := strings.NewReader(output)
decoder := xml.NewDecoder(outputReader)
for t, err = decoder.Token(); err == nil; t, err = decoder.Token() {
switch token := t.(type) {
// 处理元素开始(标签)
case xml.StartElement:
name := token.Name.Local
fmt.Printf("Token name: %s\n", name)
for _, attr := range token.Attr {
attrName := attr.Name.Local
attrValue := attr.Value
fmt.Printf("An attribute is: %s %s\n", attrName, attrValue)
}
// 处理元素结束(标签)
case xml.EndElement:
fmt.Printf("Token of '%s' end\n", token.Name.Local)
// 处理字符数据(这里就是元素的文本)
case xml.CharData:
content := string([]byte(token))
fmt.Printf("This is the content: %v\n", content)
result = content
default:
// ...
}
}
fmt.Println("result:", result) //
}
//POST到webService
func PostWebService(url string, method string, value string) string {
res, err := http.Post(url, "text/xml; charset=utf-8", bytes.NewBuffer([]byte(value)))
//这里随便传递了点东西
if err != nil {
fmt.Println("post error", err)
}
data, err := ioutil.ReadAll(res.Body)
//取出主体的内容
if err != nil {
fmt.Println("read error", err)
}
res.Body.Close()
// fmt.Printf("result----%s", data)
return ByteToString(data)
}
func ByteToString(res []byte) string {
return string(res)
}
// 生成的xml 文件需要你自己抓取请求的xml 文件,然后获取结果信息
func CreateSOAPXml(nameSpace string, methodName string, valueStr string) string {
soapBody := `<?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">`
soapBody += "<S:Body>"
soapBody += "<" + methodName + " xmlns=\"" + nameSpace + "\">"
//以下是具体参数
soapBody += "<in0>" + valueStr + "</in0>"
soapBody += "</" + methodName + "></S:Body></S:Envelope>"
//=============抓取的xml请求文件;==========
// soapBody := `<?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
// <S:Body>
// <signature xmlns="http://itrus.com/itrusUtil">
// <in0>{"Message":"BEB9737BE123E048EECEF35D249CBBDA","password":"payments","pfx":"2016123016PT27955416.pfx"}</in0>
// </signature></S:Body></S:Envelope>`
return soapBody
}
// Htmlquote returns quoted html string.
func Htmlquote(src string) string {
//HTML编码为实体符号
/*
Encodes `text` for raw use in HTML.
>>> htmlquote("<'&\\">")
'<'&">'
*/
text := string(src)
text = strings.Replace(text, "&", "&", -1) // Must be done first!
text = strings.Replace(text, "<", "<", -1)
text = strings.Replace(text, ">", ">", -1)
text = strings.Replace(text, "'", "'", -1)
text = strings.Replace(text, "\"", """, -1)
text = strings.Replace(text, "“", "“", -1)
text = strings.Replace(text, "”", "”", -1)
text = strings.Replace(text, " ", " ", -1)
return strings.TrimSpace(text)
}
/*
以上都是个人理解,有问题大家一起讨论
*/
有疑问加站长微信联系(非本文作者)