android http basic authorization,C# HTTP协议的基本认证(HTTP Basic Authorization)

HTTP基本认证的格式,HTTP请求时使用HTTP头来传递参数:

明文格式: "username" + ":" + "password",需要Base64编码明文。

HTTP头设置:Authorization: Basic [Base64编码内容]

相当于HTML的写法:,这下子我们应该都理解了。

任何支持HTTP协议的语言,均可以使用这种格式,进行HTTP协议基本认证,其缺点就是安全性较低。

C#基本认证实现代码:using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Net;

using System.Text;

using System.Threading.Tasks;

namespace What21

{

public class What21Main

{

static void Main(string[] args)

{

try

{

string url = "http://product.what21.com/";

// 创建HttpWebRequest对象

HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);

// 设置GET调用方法

httpRequest.Method = "GET";

// 设置HTTP头Http Basic认证

string authorization = "admin" + ":" + "admin";

string base64 = Convert.ToBase64String(Encoding.Default.GetBytes(authorization));

httpRequest.Headers.Add("Authorization", "Basic " + base64);

// HttpWebRequest发起调用

using (HttpWebResponse myResponse = (HttpWebResponse)httpRequest.GetResponse())

{

// StreamReader对象

StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);

// 返回结果

string responseString = sr.ReadToEnd();

Console.WriteLine("调用结果:" + responseString);

}

}

catch (Exception e)

{

Console.WriteLine(e.StackTrace);

}

Console.WriteLine("what21.com prompt, Press any key to continue . . . ");

Console.ReadKey(true);

}

}

}

HTTP协议请求头信息:

GET /index.html HTTP/1.1

Host: 127.0.0.1

Authorization: Basic YWRtaW46YWRtaW4=

HTTP基本认证是HTTP协议的规定,是个协议标准。平时我们使用的较为少,认证所传递的用户名和密码是Base64编码后的内容,非常容易解码,造成了极大的不安全性。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值