色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

訪問需要HTTP Basic Authentication認證的資源的各種語言的實現

無聊想調用下嘀咕的api的時候,發現需要HTTP Basic Authentication,就看了下。

什么是HTTP Basic Authentication?直接看http://en.wikipedia.org/wiki/Basic_authentication_scheme吧。

在你訪問一個需要HTTP Basic Authentication的URL的時候,如果你沒有提供用戶名和密碼,服務器就會返回401,如果你直接在瀏覽器中打開,瀏覽器會提示你輸入用戶名和密碼(google瀏覽器不會,bug?)。你可以嘗試點擊這個url看看效果:http://api.minicloud.com.cn/statuses/friends_timeline.xml

要在發送請求的時候添加HTTP Basic Authentication認證信息到請求中,有兩種方法:

下面來看下對于第一種在請求中添加Authorization頭部的各種語言的實現代碼。

先看.NET的吧:

string username="username";
string password="password";
//注意這里的格式哦,為 "username:password"
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))); 

WebResponse wr 
= myReq.GetResponse();
Stream receiveStream 
= wr.GetResponseStream();
StreamReader reader 
= new StreamReader(receiveStream, Encoding.UTF8);
string content = reader.ReadToEnd(); 

it知識庫訪問需要HTTP Basic Authentication認證的資源的各種語言的實現,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 盐城市| 昭平县| 闽侯县| 弋阳县| 芦山县| 鄂温| 门源| 秦安县| 崇信县| 武宁县| 昭觉县| 洛宁县| 托里县| 南安市| 上饶县| 道孚县| 晋宁县| 微博| 渭源县| 广西| 兰溪市| 吉林省| 新宾| 介休市| 泾阳县| 新竹市| 惠水县| 丹棱县| 襄樊市| 烟台市| 黑龙江省| 华阴市| 广昌县| 墨竹工卡县| 榕江县| 曲周县| 博野县| 高青县| 利川市| 隆安县| 韩城市|