private async void RequestAsync() { NCookie ncookie = new NCookie(); ncookie.Refere = "http://scholar.google.com"; ncookie.LastestRequestTime = DateTime.Now; CookieContainer cookiecontainer = new CookieContainer(); using (HttpClientHandler httpclienthandler = new HttpClientHandler() { CookieContainer = cookiecontainer }) using (HttpClient httpclient = new HttpClient(httpclienthandler)) { httpclient.DefaultRequestHeaders.Add("Accept", "text/html, application/xhtml+xml, */*"); httpclient.DefaultRequestHeaders.Add("Accept-Language", "zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3"); httpclient.DefaultRequestHeaders.Add("User-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"); httpclient.DefaultRequestHeaders.Add("Host", "scholar.google.com"); httpclient.DefaultRequestHeaders.Add("DNT", "1"); httpclient.DefaultRequestHeaders.Add("Connection", "Keep-Alive"); string url = "http://scholar.google.com"; HttpResponseMessage response = await httpclient.GetAsync(url); Uri uri = new Uri(url); var cookies = cookiecontainer.GetCookies(uri).Cast<Cookie>(); foreach (var cookie in cookies) { ncookie.Cookies.Add(cookie.Name, cookie.Value); } } lock (obj) { NCookies.Add(ncookie); } }
public string GetHtml(string url) { NCookie ncookie = cookieManager.Get(); ncookie.Refere = url; CookieContainer cookiecontainer = new CookieContainer(); foreach (var nc in ncookie.Cookies) { cookiecontainer.Add(new Cookie(nc.Key, nc.Value, "/", "scholar.google.com")); } using (HttpClientHandler handler = new HttpClientHandler() { CookieContainer = cookiecontainer, UseCookies = true }) using (HttpClient httpclient = new HttpClient(handler)) { httpclient.DefaultRequestHeaders.Add("Accept", "text/html, application/xhtml+xml, */*"); httpclient.DefaultRequestHeaders.Add("Accept-Language", "zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3"); httpclient.DefaultRequestHeaders.Add("User-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"); httpclient.DefaultRequestHeaders.Add("Host", "scholar.google.com"); httpclient.DefaultRequestHeaders.Add("DNT", "1"); httpclient.DefaultRequestHeaders.Add("Connection", "Keep-Alive"); return(httpclient.GetStringAsync(url).Result); } }
public NCookie Get() { //RemoveExpire(); NCookie cookie = NCookies[0]; cookie.LastestRequestTime = DateTime.Now; NCookies = NCookies.OrderBy(x => x.LastestRequestTime).ToList <NCookie>(); TotalRequestCount++; if (TotalRequestCount <= MaxRequestCount || (TotalRequestCount > MaxRequestCount && TotalRequestCount % MaxRequestCount == 0)) { Request(); } return(cookie); }
private async void RequestAsync() { NCookie ncookie = new NCookie(); ncookie.Refere = "http://scholar.google.com"; ncookie.LastestRequestTime = DateTime.Now; CookieContainer cookiecontainer = new CookieContainer(); using (HttpClientHandler httpclienthandler = new HttpClientHandler() { CookieContainer = cookiecontainer }) using (HttpClient httpclient = new HttpClient(httpclienthandler)) { httpclient.DefaultRequestHeaders.Add("Accept", "text/html, application/xhtml+xml, */*"); httpclient.DefaultRequestHeaders.Add("Accept-Language", "zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3"); httpclient.DefaultRequestHeaders.Add("User-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"); httpclient.DefaultRequestHeaders.Add("Host", "scholar.google.com"); httpclient.DefaultRequestHeaders.Add("DNT", "1"); httpclient.DefaultRequestHeaders.Add("Connection", "Keep-Alive"); string url = "http://scholar.google.com"; HttpResponseMessage response = await httpclient.GetAsync(url); Uri uri = new Uri(url); var cookies = cookiecontainer.GetCookies(uri).Cast <Cookie>(); foreach (var cookie in cookies) { ncookie.Cookies.Add(cookie.Name, cookie.Value); } } lock (obj) { NCookies.Add(ncookie); } }