英文:
How do I get to authenticate and send xml through http in Go (golang) SOAP
问题
我正在尝试通过http(SOAP)连接到服务器。
但是我遇到了一个错误:401 - 未经授权:由于凭据无效而拒绝访问
。
那么,在发送请求之前,我该如何发送凭据?我已经做到了这一点。
package main
import (
"net/http"
"bytes"
"fmt"
"io/ioutil"
)
func main() {
buf := []byte(`<?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>
<ClientGetByGuid xmlns="http://tempuri.org/">
<guid>fc40a874-2902-4539-b8e7-6aa7084644ec</guid>
</ClientGetByGuid>
</soap:Body>
</soap:Envelope>`)
body := bytes.NewBuffer(buf)
r, _ := http.Post("http://mywebsite.com.br/service.svc?wsdl", "text/xml", body)
response, _ := ioutil.ReadAll(r.Body)
fmt.Println(string(response))
}
谢谢。
英文:
I'm trying to connect to a server through http (SOAP).
But I'm getting an error: 401 - Unauthorized: Access is denied due to invalid credentials
.
So, how can I send the credentials before the request? I've got here so far.
package main
import (
"net/http"
"bytes"
"fmt"
"io/ioutil"
)
func main() {
buf := []byte(`<?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>
<ClientGetByGuid xmlns="http://tempuri.org/">
<guid>fc40a874-2902-4539-b8e7-6aa7084644ec</guid>
</ClientGetByGuid>
</soap:Body>
</soap:Envelope>`)
body := bytes.NewBuffer(buf)
r, _ := http.Post("http://mywebsite.com.br/service.svc?wsdl", "text/xml", body)
response, _ := ioutil.ReadAll(r.Body)
fmt.Println(string(response))
}
Thanks.
答案1
得分: 4
如果你在谈论HTTP基本身份验证,创建一个Request
对象并使用SetBasicAuth(username, password string)方法。
更多信息请参见这个问题。
英文:
If you are talking about HTTP Basic Auth, create a Request
object and use the SetBasicAuth(username, password string) method.
See this question for more information.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论