C HttpClient Cookie verify the solution
- 2020-05-07 20:21:09
- OfStack
Self - implemented cookie validation, remote value example
The following code can be used in conjunction with HttpClient to achieve cross-domain (read and write of cookie)
/ / verification
-- write the cookie file serialized back
-- read the file deserialize cookies assigned to httpClient by cookies
The following code can be used in conjunction with HttpClient to achieve cross-domain (read and write of cookie)
/ / verification
HttpClient httpClient = new HttpClient(url, null, true);
httpClient.PostingData.Add(key,value);// Login username
httpClient.PostingData.Add(key,value);// password
string str = httpClient.GetString();
-- write the cookie file serialized back
CookieCollection cookies = httpClient.Context.Cookies;// save 1 A global cookie file
FileStream fileStream = new FileStream("xxx.dat", FileMode.Create);
BinaryFormatter b = new BinaryFormatter();
b.Serialize(fileStream, cookies);
fileStream.Close();
-- read the file deserialize cookies assigned to httpClient by 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");// The value of url
httpClient.Context.Cookies = cookies;
string str = httpClient.GetString();