一、解析soap数据
package main import ("fmt" "encoding/xml"
) type Envelope struct { XMLName xml.Name Header Header
} type Header struct { XMLName xml.Name `xml:"Header"` Security Security `xml:"Security"`
} type Security struct { XMLName xml.Name `xml:"Security"` MustUnderstand string `xml:"mustUnderstand,attr"` WSSE string `xml:"wsse,attr"` SOAP string `xml:"soap,attr"` UsernameToken struct { XMLName xml.Name `xml:"UsernameToken"` Username string `xml:"Username"` Password string `xml:"Password"` }
} func main() { Soap := []byte(`<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <wsse:Security soap:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <wsse:UsernameToken> <wsse:Username>USERNAME</wsse:Username> <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">SECRET</wsse:Password> </wsse:UsernameToken> </wsse:Security> </SOAP-ENV:Header> <SOAP-ENV:Body xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <OTA_HotelAvailNotifRQ xmlns="http://www.opentravel.org/OTA/2003/05" EchoToken="abc123" Version="1.0" TimeStamp="2005-08-01T09:30:47+08:00"> <AvailStatusMessages HotelCode="HOTEL"> <AvailStatusMessage BookingLimit="10"> <StatusApplicationControl Start="2010-01-01" End="2010-01-14" InvTypeCode="A1K" RatePlanCode="GLD"/> </AvailStatusMessage> </AvailStatusMessages> </OTA_HotelAvailNotifRQ> </SOAP-ENV:Body> </SOAP-ENV:Envelope>`) res := &Envelope{} err := xml.Unmarshal(Soap, res) fmt.Println(res.Header.Security.UsernameToken.Username, err)
}
二、修改其中部分数据
1、代码一
package mainimport ("fmt""strings"
)var xmlRaw = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core- properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><dc:title></dc:title>
<dc:subject></dc:subject><dc:creator>John Kerry</dc:creator>
<cp:keywords></cp:keywords><dc:description></dc:description>
<cp:lastModifiedBy>TomHanks</cp:lastModifiedBy><cp:revision>6</cp:revision>
<dcterms:created xsi:type="dcterms:W3CDTF">2018-02-
20T18:08:00Z</dcterms:created><dcterms:modified
xsi:type="dcterms:W3CDTF">2018-04-24T19:43:00Z</dcterms:modified>
</cp:coreProperties>`type decoder struct {}func main() {fmt.Println(strings.Replace(xmlRaw, "TomHanks", "Jerry Garcia", 1))
}
2、代码二
package mainimport ("encoding/xml""fmt"
)var xmlRaw = []byte(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core- properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><dc:title></dc:title>
<dc:subject></dc:subject><dc:creator>John Kerry</dc:creator>
<cp:keywords></cp:keywords><dc:description></dc:description>
<cp:lastModifiedBy>TomHanks</cp:lastModifiedBy><cp:revision>6</cp:revision>
<dcterms:created xsi:type="dcterms:W3CDTF">2018-02-
20T18:08:00Z</dcterms:created><dcterms:modified
xsi:type="dcterms:W3CDTF">2018-04-24T19:43:00Z</dcterms:modified>
</cp:coreProperties>`)type decoder struct {Keywords string `xml:"keywords"`LastModifiedBy string `xml:"lastModifiedBy"`//.. more xml
}func main() {d := decoder{}if err := xml.Unmarshal(xmlRaw, &d); err != nil {panic(err)}fmt.Println(d.LastModifiedBy)d.LastModifiedBy = "Jerry Garcia"bytes, err := xml.Marshal(d)if err != nil {panic(err)}fmt.Println(string(bytes))}package mainimport ("encoding/xml""fmt"
)var xmlRaw = []byte(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core- properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><dc:title></dc:title>
<dc:subject></dc:subject><dc:creator>John Kerry</dc:creator>
<cp:keywords></cp:keywords><dc:description></dc:description>
<cp:lastModifiedBy>TomHanks</cp:lastModifiedBy><cp:revision>6</cp:revision>
<dcterms:created xsi:type="dcterms:W3CDTF">2018-02-
20T18:08:00Z</dcterms:created><dcterms:modified
xsi:type="dcterms:W3CDTF">2018-04-24T19:43:00Z</dcterms:modified>
</cp:coreProperties>`)type decoder struct {Keywords string `xml:"keywords"`LastModifiedBy string `xml:"lastModifiedBy"`//.. more xml
}func main() {d := decoder{}if err := xml.Unmarshal(xmlRaw, &d); err != nil {panic(err)}fmt.Println(d.LastModifiedBy)d.LastModifiedBy = "Jerry Garcia"bytes, err := xml.Marshal(d)if err != nil {panic(err)}fmt.Println(string(bytes))}
三、构造soap数据,发送http请求
package mainimport ("bytes""encoding/base64""encoding/xml""fmt""io/ioutil""log""net/http""os"
)type FileSt struct {Str stringI int
}type Envelope struct {XMLName xml.Name `xml:"soap:Envelope"`SoapNs string `xml:"xmlns:soapenv,attr"`Soapwss string `xml:"xmlns:wss,attr,omitempty"`Body *Body
}
//<ns2:dmFileByteResponse xmlns:ns2="http://wssmcommlower/"creturns!"code".1"messade"."也""data","S4g50u-62ZICAN2WTI5NTEOJICA 5550ICAX21MT5NTEWD0ICA572CP5m21CAX20MT5NTEYICA 5 EH52CAX0WTI5NTEYOO="%<EtU
///ns2:dmFileByteResponse>
type Body struct {XMLName xml.Name `xml:"soap:Body"`Req *Request
}
type Request struct {XMLName xml.Name `xml:"wss:dmFileByte"`PolicyName string `xml:"policyName"`SrcFile string `xml:"srcFile"`SrcFileType string `xml:"srcFileType"`
}
type EnvelopeRsp struct {XMLName xml.Name `xml:"soap:Envelope"`SoapNs string `xml:"xmlns:soapenv,attr"`Soapwss string `xml:"xmlns:wss,attr,omitempty"`Body *RspBody
}
type RspBody struct {XMLName xml.Name `xml:"soap:Body"`Resp *Response
}type Response struct {XMLName xml.Name `xml:"ns2:dmFileByteResponse"`Ns2 string `xml:"xmlns:ns2,attr"`Ret string `xml:"Return"`
}type Return struct {Code int `xml:"code"`Msg string `xml:"msg"`Data string `xml:"data"`
}func CreateSOAPRequest(src []byte) []byte {soap := &Envelope{SoapNs: "http://schemas.xmlsoap.org/soap/envelope/",Soapwss: "http://wssmcommlower/",Body: &Body{Req: &Request{PolicyName: "policyName test request",SrcFile: base64.StdEncoding.EncodeToString(src),SrcFileType: "txt",},},}output, err := xml.MarshalIndent(soap, "", " ")fmt.Println(string(output))if err != nil {log.Panic(err)}return output
}
func main() {if len(os.Args)<4{fmt.Println("usage: xxx ip port filePath")return}client:=http.Client{}ip:=os.Args[1]port:=os.Args[2]filePath:=os.Args[3]url:=fmt.Sprintf("http://%s:%s/WSSmCommLower/wsSmCommLower?wsdl",ip,port)// 设置请求地址、方法及消息体srcByte,_:=ioutil.ReadFile(filePath)req,err:=http.NewRequest("POST",url,bytes.NewBuffer(CreateSOAPRequest(srcByte)))if err!=nil{log.Panic(err)}req.Header.Add("Content-Type","application/soap+xml;charset=utf-8")// 发送请求resp,err:=client.Do(req)if err!= nil {fmt.Println("Request Error: ", err)return}defer resp.Body.Close()// 解析响应var result []byteresp.Body.Read(result)fmt.Println(string(result))var soapResp EnvelopeRspxml.Unmarshal(result,&soapResp)
}