使用Golang和HTTP读取FederationMetadata.xml文件。

huangapple go评论75阅读模式
英文:

Reading FederationMetadata.xml in golang using http

问题

我正在尝试使用golang通过http获取FederationMetadata.xml文件,使用的方法如下:

response, err := client.Get("https://domainc.local/FederationMetadata/2007-06/FederationMetadata.xml")

响应的主体以全数字值返回(见下文),而不是以XML格式返回。但是我可以通过Chrome将该文件下载为xml文件。

61 34 117 114 110 58 111 97 115 105 115 58 110 97 109 101 115 58
116 99 58 83 65 77 76 58 50 46 48 58 97 115 115 101 114 116 105
111 110 34 47 62 60 65 116 116 114 105 98 117 116 101 32 78 97 109
101 61 34 104 116 116 112 58 47 47 115 99 104 101 109 97 115 46 109
105 99 114 111 115 111 102 116 46 99 111 109 47 50 48 49 50 47 48
49 47 114 101 113 117 101 115 116 99 111 110 116 101 120 116 47 99 10

代码:

tr := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
client := &http.Client{Transport: tr}
response, err := client.Get("https://domainc.local/FederationMetadata/2007-06/FederationMetadata.xml")
if err != nil {
    os.Exit(1)
} else {
    defer response.Body.Close()
    fmt.Println(response.Body)
    responseXML, err := ioutil.ReadAll(response.Body)
    if err != nil {
        fmt.Printf("%s", err)
        os.Exit(1)
    }
    fmt.Println(responseXML)
}
英文:

I am trying to get the FederationMetadata.xml using http in golan using the following way.

response, err := client.Get("https://domainc.local/FederationMetadata/2007-06/FederationMetadata.xml"). 

The response body comes back as all numeric values(see below) and not in XML format. But I am able to download this file from chrome as an xml file.

61 34 117 114 110 58 111 97 115 105 115 58 110 97 109 101 115 58
116 99 58 83 65 77 76 58 50 46 48 58 97 115 115 101 114 116 105 111
110 34 47 62 60 65 116 116 114 105 98 117 116 101 32 78 97 109 101
61 34 104 116 116 112 58 47 47 115 99 104 101 109 97 115 46 109
105 99 114 111 115 111 102 116 46 99 111 109 47 50 48 49 50 47 48
49 47 114 101 113 117 101 115 116 99 111 110 116 101 120 116 47 99 10

Code:

tr := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
client := &http.Client{Transport: tr}
response, err := client.Get("https://domainc.local/FederationMetadata/2007-06/FederationMetadata.xml")
if err != nil {
	os.Exit(1)
} else {
	defer response.Body.Close()
	fmt.Println(response.Body)
	responseXML, err := ioutil.ReadAll(response.Body)
	if err != nil {
		fmt.Printf("%s", err)
		os.Exit(1)
	}
	fmt.Println(responseXML)
}

答案1

得分: 0

responseXML 是一个字节切片([]byte)。

你必须将其转换为字符串才能打印出来,可以使用以下方式之一:

fmt.Println(string(responseXML))

或者在 Printf 格式字符串中使用 %s

fmt.Printf("这是响应:\n%s\n", responseXML)
英文:

responseXML is slice of bytes (a []byte).

You must convert it to a string to print it, either with:

fmt.Println(string(responseXML))

or via %s in a Printf format string:

fmt.Printf("This is the response: \n%s\n", responseXML)

答案2

得分: 0

请使用fmt.Println(string(response.Body))代替fmt.Println(response.Body),这样应该会打印出以下内容:

... ="urn:oasis:names:tc:SAML:2.0:assertion"/><Attribute Name="http://schemas.microsoft.com/2012/01/requestcontext/c ...
英文:

Instead fmt.Println(response.Body) use fmt.Println(string(response.Body)), which should print:

... ="urn:oasis:names:tc:SAML:2.0:assertion"/><Attribute Name="http://schemas.microsoft.com/2012/01/requestcontext/c ...

huangapple
  • 本文由 发表于 2015年7月28日 01:17:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/31659320.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定