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();
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();
分类:
ASP.NET
本文转自左正博客园博客,原文链接:http://www.cnblogs.com/soundcode/p/3305571.html
,如需转载请自行联系原作者