HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("接口地址");
            request.Method = "Post";
            request.CookieContainer = new CookieContainer();
            request.ContentType = "application/json;";

            (1)设置请求Credentials
            CredentialCache credentialCache = new CredentialCache();
            credentialCache.Add(new Uri("接口地址"),"Basic", new NetworkCredential(Username,Password));
            request.Credentials = credentialCache;

            (2)设置Headers Authorization

            String authorization= Username+ ":"+Password;
            request.Headers.Add("Authorization", "Basic "+Convert.ToBase64String(Encoding.UTF8.GetBytes(authorization)));
            using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    string content = reader.ReadToEnd();
                }
            }

相关文章:

  • 2021-05-24
  • 2021-12-02
  • 2022-12-23
  • 2022-12-23
  • 2022-01-12
  • 2021-12-06
猜你喜欢
  • 2021-04-17
  • 2022-12-23
  • 2021-09-01
  • 2021-12-21
  • 2021-08-11
相关资源
相似解决方案