国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C# HttpClient Cookie驗證解決方法

2020-01-24 03:38:35
字體:
來源:轉載
供稿:網友
自實現的cookie 驗證,遠程取值的例子

以下代碼配合HttpClient使用可以實現跨域(cookie的讀寫)
//驗證
復制代碼 代碼如下:

HttpClient httpClient = new HttpClient(url, null, true);
httpClient.PostingData.Add(key,value);//登錄用戶名
httpClient.PostingData.Add(key,value);//密碼
string str = httpClient.GetString();

----寫文件 序列化傳回來的cookie
復制代碼 代碼如下:

CookieCollection cookies = httpClient.Context.Cookies;//保存一個全局的cookie文件
FileStream fileStream = new FileStream("xxx.dat", FileMode.Create);
BinaryFormatter b = new BinaryFormatter();
b.Serialize(fileStream, cookies);
fileStream.Close();

--讀文件 反序列化cookies 賦給httpClient的cookies
復制代碼 代碼如下:

FileStream fileStream = new FileStream("xxx.dat", FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryFormatter b = new BinaryFormatter();
CookieCollection cookies = b.Deserialize(fileStream) as CookieCollection;
HttpClient httpClient = new HttpClient("url");//取值的url
httpClient.Context.Cookies = cookies;
string str = httpClient.GetString();
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 滨州市| 合水县| 石渠县| 汶上县| 图们市| 玛沁县| 金平| 仁化县| 陆河县| 班玛县| 黔西| 遵义县| 永济市| 万年县| 宁安市| 崇礼县| 沐川县| 依安县| 隆昌县| 彰化县| 九江市| 元朗区| 武平县| 栖霞市| 沅江市| 共和县| 万州区| 灵山县| 九江县| 成安县| 昌黎县| 阜康市| 玉溪市| 永兴县| 曲麻莱县| 洪江市| 华坪县| 南部县| 剑阁县| 山东省| 如皋市|