按属性读取 XML

尝试使用 Golang 连接到 MS 共享点,如这里所述,所以我写了下面返回 XML 文本的内容:


package main


import (

    "bytes"

    "encoding/xml"

    "fmt"

    "io/ioutil"

    "log"

    "net/http"

    "strings"

)


func main() {

    const myurl = "https://login.microsoftonline.com/extSTS.srf"

    const username = "myuser@mydmain.com"

    const password = "mypassword"

    const endpoint = "https://mydomain.sharepoint.com/"

    const xmlbody = `

    <s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"

    xmlns:a="http://www.w3.org/2005/08/addressing"

    xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">

<s:Header>

  <a:Action s:mustUnderstand="1">http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue</a:Action>

  <a:ReplyTo>

    <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>

  </a:ReplyTo>

  <a:To s:mustUnderstand="1">https://login.microsoftonline.com/extSTS.srf</a:To>

  <o:Security s:mustUnderstand="1"

     xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">

    <o:UsernameToken>

      <o:Username>` + username + `</o:Username>

      <o:Password>` + password + `</o:Password>

    </o:UsernameToken>

  </o:Security>

</s:Header>

<s:Body>

  <t:RequestSecurityToken xmlns:t="http://schemas.xmlsoap.org/ws/2005/02/trust">

    <wsp:AppliesTo xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy">

      <a:EndpointReference>

        <a:Address>` + endpoint + `</a:Address>

      </a:EndpointReference>

    </wsp:AppliesTo>

    <t:KeyType>http://schemas.xmlsoap.org/ws/2005/05/identity/NoProofKey</t:KeyType>

    <t:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</t:RequestType>

    <t:TokenType>urn:oasis:names:tc:SAML:1.0:assertion</t:TokenType>

  </t:RequestSecurityToken>

</s:Body>

</s:Envelope>`


    resp, err := http.Post(myurl, "text/xml", strings.NewReader(xmlbody))

    if err != nil {

        log.Fatal(err)

    }

    defer resp.Body.Close()


    body, _ := ioutil.ReadAll(resp.Body)

    fmt.Println("response Body:", string(body))

}


陪伴而非守候
浏览 95回答 1
1回答

慕工程0101907

跟:&nbsp;&nbsp;&nbsp;&nbsp;Attrs&nbsp;&nbsp;&nbsp;[]xml.Attr&nbsp;`xml:"-"`您忽略了所有属性。您应该能够执行以下操作:&nbsp;&nbsp;&nbsp;ID&nbsp;string&nbsp;`xml:"Id,attr"相反,请检查您要查找的ID。您收到该错误,因为它是一个数组。不能将数组与字符串值进行比较。n.Attrs
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java