zoukankan      html  css  js  c++  java
  • 使用HttpWebRequest实现basic身份认证

    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();
                    }
                }

  • 相关阅读:
    C语言左移和右移
    mmap详谈
    eclipse插件自动生成类图
    async 和 defer 的区别
    SVN里恢复到某一天的版本操作
    解决跨域的jsonp+Java实例
    HTTP请求行、请求头、请求体等
    ajax在什么情况下会走success和error
    记阅读POST与GET的区别
    记一些快捷键
  • 原文地址:https://www.cnblogs.com/furenjian/p/12883593.html
Copyright © 2011-2022 走看看