Answer the question
In order to leave comments, you need to log in
How to read cookie in C# WebClient?
C# application downloading a file
string link = @"http://localhost:2613/siteapi/getfile";
WebClient webClient = new WebClient();
webClient.Headers.Add(HttpRequestHeader.Cookie, "accesscode=123456");
webClient.DownloadFileAsync(new Uri(link), @"c:\Download\" + "file.txt");
Answer the question
In order to leave comments, you need to log in
No way, because you don’t work with the server’s response, the DownloadFileAsync method hides it from you, for full HTTP support, go to HttpWebRequest or RestSharp, by the way, the WebClient inside is also not ideal, it seems that the files didn’t download, but rewrote it to HttpWebRequest, and everything went.
google webclient cookies
It is possible and quite easy.
This is easy to do with HttpClient using CookieContainer
var cookieContainer = new CookieContainer();
_handler = new HttpClientHandler {CookieContainer = cookieContainer};
_client = new HttpClient(_handler);
private void SaveCookie()
{
var cookies = _handler.CookieContainer.GetCookies(new Uri(BaseUrl));
var cookie = cookies["PHPSESSID"];
using (var writer = new StreamWriter(_cookieFile))
{
writer.Write(cookie.ToJson());
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question