Http Authorization Basic

一、

public HttpStatusCode http_authorization_basic(string username, string password)
{
  WebRequest myReq = WebRequest.Create(url);
  string usernamePassword = username + ":" + password;
  CredentialCache mycache = new CredentialCache();
  mycache.Add(new Uri(url), "Basic", new NetworkCredential(username, password));
  myReq.Credentials = mycache;
  myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));
  HttpWebResponse wr = (HttpWebResponse)myReq.GetResponse();
  return wr.StatusCode;

二、

WebClient wc = new WebClient();
wc.Credentials = new System.Net.NetworkCredential("admin", "admin");
 string url = "http://192.168.10.1/";
string response = Encoding.UTF8.GetString(wc.DownloadData(url)); 

posted @ 2012-01-04 11:20  cybean  阅读(690)  评论(0编辑  收藏  举报