public static async Task <string> GetWebContent(string url) { using (var myHttpClientHandler = new MyHttpClientHandler(cookieContainer)) using (var httpClient = new HttpClient(myHttpClientHandler)) { Console.WriteLine($"Starting connect {url}"); httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml+json"); httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"); //httpClient.DefaultRequestHeaders.Add("Host", "facebook.com"); //httpClient.DefaultRequestHeaders.Add("Connection", "keep-alive"); HttpResponseMessage response = await httpClient.GetAsync(url); response.EnsureSuccessStatusCode(); byte[] bytes = await response.Content.ReadAsByteArrayAsync(); //Encoding encoding = Encoding.GetEncoding("windows-1251"); Encoding encoding = Encoding.GetEncoding("utf-8"); string html = encoding.GetString(bytes, 0, bytes.Length); return(html); //HttpResponseMessage response = await httpClient.GetAsync(url); //response.EnsureSuccessStatusCode(); //string htmltext = await response.Content.ReadAsStringAsync(); //return htmltext; } }
public static async Task <string> GetWebContent(string url) { using (var myHttpClientHandler = new MyHttpClientHandler(cookieContainer)) using (var httpClient = new HttpClient(myHttpClientHandler)) { Console.WriteLine($"Starting connect {url}"); httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml+json"); httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"); HttpResponseMessage response = await httpClient.GetAsync(url); response.EnsureSuccessStatusCode(); string htmltext = await response.Content.ReadAsStringAsync(); return(htmltext); } }
public static async Task <string> GetWebContent(string url, string cookieStr) { CookieContainer cookieContainer = new CookieContainer(); // Sử dụng CookieContainer riêng, để lưu lại Cookie - hoặc thêm cookie var arrCookie = cookieStr.Split(';'); foreach (var cookie in arrCookie) { var arr = cookie.Split('='); if (arr.Length > 1) { cookieContainer.Add(new Uri(url), new Cookie(arr[0].Trim(), arr[1].Trim())); } } using (var myHttpClientHandler = new MyHttpClientHandler(cookieContainer)) using (var httpClient = new HttpClient(myHttpClientHandler)) { Console.WriteLine($"Starting connect {url}"); httpClient.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml+json"); httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"); //httpClient.DefaultRequestHeaders.Add("Host", "facebook.com"); //httpClient.DefaultRequestHeaders.Add("Connection", "keep-alive"); HttpResponseMessage response = await httpClient.GetAsync(url); response.EnsureSuccessStatusCode(); byte[] bytes = await response.Content.ReadAsByteArrayAsync(); //Encoding encoding = Encoding.GetEncoding("windows-1251"); Encoding encoding = Encoding.GetEncoding("utf-8"); string html = encoding.GetString(bytes, 0, bytes.Length); return(html); //HttpResponseMessage response = await httpClient.GetAsync(url); //response.EnsureSuccessStatusCode(); //string htmltext = await response.Content.ReadAsStringAsync(); //return htmltext; } }