doumaojin4008
doumaojin4008
2017-03-06 07:36

如何在网络中使用网络凭据发送https / http请求

  • https
  • beego
已采纳

in 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;

and in go i can add only add these

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

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

</div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongzine3782 dongzine3782 4年前

    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("Authorization", "Basic sdfsfscefewfewfew")
    
    点赞 评论 复制链接分享