如何在Go语言中使用网络凭据发送HTTPS/HTTP请求

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

how to send https/http request with network credentials in go

问题

在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中添加网络凭据的挑战是什么?

英文:

in c#

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

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

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

<!-- end snippet -->

and in go i can add only add these

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

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

<!-- end snippet -->

here in go the challenge is how to add network credentials in go

答案1

得分: 0

根据我在C#文档中的了解,你所提到的"keys"可能指的是用户名和密码。你可以使用类似的方法http.Requestmethod来设置基本身份验证的用户名和密码。

// username的值与utility.commonkey相同
// password是对应的明文密码
req.SetBasicAuth(username, password);

同时,请确保从你的Go代码中删除以下行:

req.Header.Set("Authorization", "Basic sdfsfscefewfewfew");
英文:

From what I see in the C# docs what you're referring at as keys, could only be username and password. You could use the analogue http.Request's method to set username and password for basic auth.

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

Don't forget to remove the following line from your Go code as well

req.Header.Set(&quot;Authorization&quot;, &quot;Basic sdfsfscefewfewfew&quot;)

huangapple
  • 本文由 发表于 2017年3月6日 15:36:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/42619977.html
匿名

发表评论

匿名网友

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

确定