Can't download the bags from the file.
-
There's the following code:
private static readonly HttpClientHandler handler = new HttpClientHandler { CookieContainer = new CookieContainer(), UseCookies = true }; private static readonly HttpClient httpClient = new HttpClient(handler);
public Class1()
{
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 UBrowser/6.0.1308.1016 Safari/537.36");httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Language", "ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4");
}
Preservation of dock
json
:string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string filename = Path.Combine(path, "test/cookies.bin");if (File.Exists(filename)) File.Delete(filename);
var cookies = handler.CookieContainer.GetCookies(new Uri("https://site.ru/"));
using var fs = new FileStream(filename, FileMode.OpenOrCreate);
await JsonSerializer.SerializeAsync(fs, cookies);
Cook loading:
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string filename = Path.Combine(path, "test/cookies.bin");using var fs = new FileStream(filename, FileMode.Open);
var cookies = await JsonSerializer.DeserializeAsync<List<Cookie>>(fs);
foreach (var cookie in cookies)
{
handler.CookieContainer.Add(cookie);
}
Cookie in the file:
[{"Comment":"","CommentUri":null,"HttpOnly":true,"Discard":false,"Domain":"site.ru","Expired":false,"Expires":"0001-01-01T00:00:00","Name":"__RequestVerificationToken","Path":"/","Port":"","Secure":true,"TimeStamp":"2020-10-09T13:33:07.1633063+03:00","Value":"D4cK7wXeq6KThXJG0uZJ8gxUZQ_jGr1pNUwrCuJEhlKVeQHiLQzbvdkZXxmcWT8oYRt4ipPUes4D0dMTxbgY_2yZnJUZxM03F7n0AVtFShU1","Version":0},{"Comment":"","CommentUri":null,"HttpOnly":true,"Discard":false,"Domain":"site.ru","Expired":false,"Expires":"2020-10-11T13:33:08+03:00","Name":".ASPXAUTH","Path":"/","Port":"","Secure":false,"TimeStamp":"2020-10-09T13:33:07.6753356+03:00","Value":"824040565E5BCCD7A57ECDFC3BF4A3D5BEFF477161636356924B0340E53F1527470E6513BE272EDB9392A05C652542ED27F5A001D92DFDE5F395BD1D75708F60CCF2DE2D40ACDF1B8D82743D8CED67713FE85653BB04716B534FA3CEAD261FC9BED4BA50A21C9D2C989FDB2EF1D9ADE4","Version":0}]
GET
Request through browser:GET
Request through programme:The thing is, after the dock's download, I don't get the page that I have to do, but I get the copy page. Apparently, the problem is, I'm kind of "notloading" a bitch.
.ASPXAUTH
..and the image of the browser shows other pieces (_ym_uid
I can't understand whether they're binding or not?There's no business with a bunch of people before, please tell me what's going on?
UPD:
private static readonly HttpClientHandler handler = new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
AllowAutoRedirect = true
};
private static readonly HttpClient httpClient = new HttpClient(handler);public HttpManager()
{
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 UBrowser/6.0.1308.1016 Safari/537.36");
httpClient.DefaultRequestHeaders.AcceptEncoding.ParseAdd("gzip, deflate, br");
}
I get the pieces now:
var cookies = handler.CookieContainer.GetAllCookies();
I'm so loaded:
List<Cookie> cookies = await JsonSerializer.DeserializeAsync<List<Cookie>>(fs);
foreach (var cookie in cookies)
{
// не загружать, если кука заэкспайрилась
if (!cookie.Expired && (cookie.Expires == DateTime.MinValue || cookie.Expires > DateTime.Now))
handler.CookieContainer.Add(cookie);
}
This class was borrowed by reference, which was written in the commentaries:
public static class CookieContainerExtensions
{
// Забирает все куки из контейнера
public static CookieCollection GetAllCookies(this CookieContainer container)
{
CookieCollection allCookies = new CookieCollection();
IDictionary domains = (IDictionary)container.GetType()
.GetRuntimeFields()
.FirstOrDefault(x => x.Name == "m_domainTable")
.GetValue(container);foreach (object field in domains.Values) { IDictionary values = (IDictionary)field.GetType() .GetRuntimeFields() .FirstOrDefault(x => x.Name == "m_list") .GetValue(field); foreach (CookieCollection cookies in values.Values) { allCookies.Add(cookies); } } return allCookies; }
}
Unfortunately, I actually get what I got earlier. See, I'm missing something. ♪
-
For the correct downloading of the dock (specifically for my site), there was only one piece to keep--
.ASPXAUTH
and then we'll let her down.The final code was:
private static readonly HttpClientHandler handler = new HttpClientHandler { CookieContainer = new CookieContainer(), UseCookies = true }; private static readonly Uri baseUri = new Uri("https://e.site.ru"); private static readonly HttpClient httpClient = new HttpClient(handler) { BaseAddress = baseUri };
public static async void SaveCookies()
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string filename = Path.Combine(path, "test/cookies.bin");var cookies = handler.CookieContainer.GetCookies(baseUri); using var sw = new StreamWriter(filename, false); // сохраняется лишь сам ключ, без имени await sw.WriteAsync(cookies[1].Value);
}
public static async void LoadCookies()
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string filename = Path.Combine(path, "test/cookies.bin");using var sr = new StreamReader(filename); var cookie_value = await sr.ReadToEndAsync(); handler.CookieContainer.Add(baseUri, new Cookie(".ASPXAUTH", cookie_value));
}