zoukankan      html  css  js  c++  java
  • HTTP Basic 验证客户端 C#实现笔记

    HTTP Basic 验证客户端的原理:
    把HTTP头重的ContentType设置为:application/x-www-form-urlencoded
    如果HTTP头没有Authorization,那么添加,并把这个设置为“Basic 用户名:密码”字符串组合的Base64编码。

    代码片段:

    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

    request.Method 
    = "GET";
    request.ContentType 
    = "application/x-www-form-urlencoded";
    request.Credentials 
    = CredentialCache.DefaultCredentials;

    //获得用户名密码的Base64编码
    string code= Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}""username""password")));

    //添加Authorization到HTTP头
    request.Headers.Add("Authorization""Basic " + code);

    HttpWebResponse response 
    = (HttpWebResponse)request.GetResponse();
    StreamReader reader 
    = new StreamReader(response.GetResponseStream());

    string content= reader.ReadToEnd();
  • 相关阅读:
    第五周总结
    第四周总结
    第三周总结
    第二周总结
    第一周总结
    暑假学习进度八
    使用nmtui文本框方式修改IP
    Linux 忘记密码配置
    关于公网IP和内网IP
    常见API编写方式(三种)
  • 原文地址:https://www.cnblogs.com/format/p/1595108.html
Copyright © 2011-2022 走看看