du0204 2013-06-24 14:41
浏览 302

如何在Go(golang)SOAP中通过http进行身份验证和发送xml

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条回答 默认 最新

  • donglu1971 2013-06-24 15:08
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!