【问题标题】:how to send https/http request with network credentials in go如何在 go 中发送带有网络凭据的 https/http 请求
【发布时间】:2017-03-06 07:36:44
【问题描述】:

在c#中

var uri = new Uri("https://demo.com/");
                var encoding = new UTF8Encoding();
                var request = (HttpWebRequest)WebRequest.Create(uri);
                request.Method = "POST";

                request.Credentials = new NetworkCredential(utility.commonkey, utility.commonsecrerkey);
                request.ContentType = "application/json";
                request.Accept = "application/vnd.demo+json;version=3;";
                request.ContentLength = encoding.GetByteCount(json);
                request.Headers.Add("data", json);
                HttpWebResponse response = null;

在go中我只能添加这些

req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Authorization", "Basic sdfsfscefewfewfew")
	req.Header.Set("Accept", "application/vnd.demo+json; version=3;")

这里的挑战是如何在 go 中添加网络凭据

【问题讨论】:

  • 那么您遇到的问题是什么?只要sdfsfscefewfewfew 对应于base64 中的user:password,这应该可以正常工作。
  • 需要添加request.Credentials = new NetworkCredential(utility.commonkey, utility.commonsecrerkey);也但不确定如何在go中做到这一点
  • @Havelock,我应该在请求的哪个参数中添加这个网络凭据......请参考上面的c#代码......
  • utility.commonkeyutility.commonsecrerkey 代表什么?是用户名和密码吗?
  • 这些键基本上是......服务器决定服务哪个api的键

标签: go https beego networkcredentials


【解决方案1】:

根据我在the C# docs 中看到的您所指的密钥,只能是用户名和密码。您可以使用模拟http.Requestmethod 来设置基本身份验证的用户名和密码。

// username has the same value as utility.commonkey
// password is the corresponding password in plain text
req.SetBasicAuth(username, password);

不要忘记从你的 Go 代码中删除以下行

req.Header.Set("Authorization", "Basic sdfsfscefewfewfew")

【讨论】:

  • 是的,它是真的......它实际上是用户名和密码类型
猜你喜欢
  • 1970-01-01
  • 2020-06-13
  • 2013-04-27
  • 2015-12-30
  • 2011-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-28
相关资源
最近更新 更多