现在的位置: 首页 > 综合 > 正文

Http Authorization Basic

2012年05月25日 ⁄ 综合 ⁄ 共 700字 ⁄ 字号 评论关闭

一、

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

抱歉!评论已关闭.