public void GetCookies_AddCookiesWithImplicitDomain_CookiesReturnedOnlyForExactDomainMatch() { const string SchemePrefix = "http://"; const string OriginalDomain = "contoso.com"; var container = new CookieContainer(); var cookie1 = new Cookie(CookieName1, CookieValue1); var cookie2 = new Cookie(CookieName2, CookieValue2) { Version = 1 }; var uri = new Uri(SchemePrefix + OriginalDomain); container.Add(uri, cookie1); container.Add(uri, cookie2); var cookies = container.GetCookies(uri); Assert.Equal(2, cookies.Count); Assert.Equal(OriginalDomain, cookies[CookieName1].Domain); Assert.Equal(OriginalDomain, cookies[CookieName2].Domain); uri = new Uri(SchemePrefix + "www." + OriginalDomain); cookies = container.GetCookies(uri); Assert.Equal(0, cookies.Count); uri = new Uri(SchemePrefix + "x.www." + OriginalDomain); cookies = container.GetCookies(uri); Assert.Equal(0, cookies.Count); uri = new Uri(SchemePrefix + "y.x.www." + OriginalDomain); cookies = container.GetCookies(uri); Assert.Equal(0, cookies.Count); uri = new Uri(SchemePrefix + "z.y.x.www." + OriginalDomain); cookies = container.GetCookies(uri); Assert.Equal(0, cookies.Count); }
private static CookieContainer CreateCount11Container() { CookieContainer cc1 = new CookieContainer(); // Add(Cookie) cc1.Add(c1); cc1.Add(c2); cc1.Add(c3); cc1.Add(c4); // Add(CookieCollection) CookieCollection cc2 = new CookieCollection(); cc2.Add(c5); cc2.Add(c6); cc2.Add(c7); cc1.Add(cc2); // Add(Uri, Cookie) cc1.Add(u4, c8); cc1.Add(u4, c9); // Add(Uri, CookieCollection) cc2 = new CookieCollection(); cc2.Add(c10); cc2.Add(c11); cc1.Add(u5, cc2); return cc1; }
public void GetCookies_AddCookieVersion0WithExplicitDomain_CookieReturnedForDomainAndSubdomains() { const string SchemePrefix = "http://"; const string OriginalDomain = "contoso.com"; var container = new CookieContainer(); var cookie1 = new Cookie(CookieName1, CookieValue1) { Domain = OriginalDomain }; container.Add(new Uri(SchemePrefix + OriginalDomain), cookie1); var uri = new Uri(SchemePrefix + OriginalDomain); var cookies = container.GetCookies(uri); Assert.Equal(1, cookies.Count); Assert.Equal(OriginalDomain, cookies[CookieName1].Domain); uri = new Uri(SchemePrefix + "www." + OriginalDomain); cookies = container.GetCookies(uri); Assert.Equal(1, cookies.Count); uri = new Uri(SchemePrefix + "x.www." + OriginalDomain); cookies = container.GetCookies(uri); Assert.Equal(1, cookies.Count); uri = new Uri(SchemePrefix + "y.x.www." + OriginalDomain); cookies = container.GetCookies(uri); Assert.Equal(1, cookies.Count); uri = new Uri(SchemePrefix + "z.y.x.www." + OriginalDomain); cookies = container.GetCookies(uri); Assert.Equal(1, cookies.Count); }
static void AddCookies(CookieCollection from, CookieContainer container) { if (from != null) { container?.Add(from); } }
private void ReadCookies(WebResponse r) { if (r is HttpWebResponse response) { CookieCollection cookies = response.Cookies; container?.Add(cookies); } }
public static void AddCookieCollection_Success() { CookieContainer cc = new CookieContainer(); CookieCollection cookieCollection = new CookieCollection(); cookieCollection.Add(new Cookie("name3", "value","/",".contoso.com")); cc.Add(cookieCollection); Assert.Equal(1, cc.Count); }
public CookiePortTest() { _cc = new CookieContainer(); _cookie = new Cookie("name", "value1", "/path", "localhost"); // use both space and comma as delimiter _cookie.Port = "\"80 110,1050, 1090 ,1100\""; _cc.Add(new Uri("http://localhost/path"), _cookie); }
public void Add_CookieVersion1AndRootDomainWithNoLeadingDot_ThrowsCookieException() { const string SchemePrefix = "http://"; const string OriginalDomain = "contoso.com"; var container = new CookieContainer(); var cookie = new Cookie(CookieName1, CookieValue1) { Version = 1, Domain = OriginalDomain }; var uri = new Uri(SchemePrefix + OriginalDomain); Assert.Throws<CookieException>(() => container.Add(uri, cookie)); }
public void AddCookies(CookieCollection cookies, Uri url) { lock (locker) { cookieStore.Add(url, cookies); } }
public static void AddCookieCollectionUri_Success() { Uri uri = new Uri("http://contoso.com"); String domain = "contoso.com"; CookieContainer cc1 = new CookieContainer(); CookieCollection cc2 = new CookieCollection(); cc2.Add(new Cookie("name1", "value") { Domain = domain }); cc2.Add(new Cookie("name2", "value") { Domain = domain }); cc1.Add(uri, cc2); Assert.Equal(2, cc1.Count); }
private string Login(string emailAddress, string password) { try { Log_External(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "Logging into google.com for " + emailAddress + ".", m_username)); // Fetch GALX HttpWebRequest galxRequest = (HttpWebRequest)WebRequest.Create(PRE_LOGIN_URL); galxRequest.ConnectionGroupName = "prelogin"; galxRequest.CookieContainer = m_cookies; HttpWebResponse galxResponse = (HttpWebResponse)galxRequest.GetResponse(); if (galxResponse.StatusCode != HttpStatusCode.OK) { galxResponse.Close(); throw new ApplicationException("Load of the Google Voice pre-login page failed with response " + galxResponse.StatusCode + "."); } else { // The pre login URL can redirect to a different URL, such as accounts.google.com, need to use the cookies from that redirect when accessing www.google.com. m_cookies.Add(new Uri(GOOGLE_COM_URL), galxResponse.Cookies); Log_External(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "Google Voice pre-login page loaded successfully.", m_username)); } StreamReader galxReader = new StreamReader(galxResponse.GetResponseStream()); string galxResponseFromServer = galxReader.ReadToEnd(); galxResponse.Close(); Match galxMatch = Regex.Match(galxResponseFromServer, @"name=""GALX""[^>]+value=""(?<galxvalue>.*?)"""); if (galxMatch.Success) { Log_External(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "GALX key " + galxMatch.Result("${galxvalue}") + " successfully retrieved.", m_username)); } else { throw new ApplicationException("Could not find GALX key on your Google Voice pre-login page, callback cannot proceed."); } Match gxfMatch = Regex.Match(galxResponseFromServer, @"name=""gxf""[^>]+value=""(?<gxfvalue>.*?)""", RegexOptions.IgnoreCase); if (gxfMatch.Success) { Log_External(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "gxf key " + gxfMatch.Result("${gxfvalue}") + " successfully retrieved.", m_username)); } else { throw new ApplicationException("Could not find GXF key on your Google Voice pre-login page, callback cannot proceed."); } // Build login request. string loginData = "Email=" + Uri.EscapeDataString(emailAddress) + "&Passwd=" + Uri.EscapeDataString(password) + "&GALX=" + Uri.EscapeDataString(galxMatch.Result("${galxvalue}")) + "&gxf=" + Uri.EscapeDataString(gxfMatch.Result("${gxfvalue}")); HttpWebRequest loginRequest = (HttpWebRequest)WebRequest.Create(LOGIN_URL); loginRequest.CookieContainer = m_cookies; loginRequest.ConnectionGroupName = "login"; loginRequest.AllowAutoRedirect = true; loginRequest.Method = "POST"; loginRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8"; loginRequest.ContentLength = loginData.Length; loginRequest.GetRequestStream().Write(Encoding.UTF8.GetBytes(loginData), 0, loginData.Length); loginRequest.Timeout = HTTP_REQUEST_TIMEOUT * 1000; // Send login request and read response stream. HttpWebResponse response = (HttpWebResponse)loginRequest.GetResponse(); if (response.StatusCode != HttpStatusCode.OK) { response.Close(); throw new ApplicationException("Login to google.com failed for " + emailAddress + " with response " + response.StatusCode + "."); } StreamReader loginResponseReader = new StreamReader(response.GetResponseStream()); string loginResponseFromServer = loginResponseReader.ReadToEnd(); response.Close(); if (Regex.Match(loginResponseFromServer, @"\<title/\>Google Accounts\</title/\>").Success) { throw new ApplicationException("Login to google.com appears to have failed for " + emailAddress + "."); } // We're now logged in. Need to load up the Google Voice page to get the rnr hidden input value which is needed for // the HTTP call requests. HttpWebRequest rnrRequest = (HttpWebRequest)WebRequest.Create(VOICE_HOME_URL); rnrRequest.ConnectionGroupName = "call"; rnrRequest.CookieContainer = m_cookies; rnrRequest.AllowAutoRedirect = true; // Send the Google Voice account page request and read response stream. response = (HttpWebResponse)rnrRequest.GetResponse(); if (response.StatusCode != HttpStatusCode.OK) { response.Close(); throw new ApplicationException("Load of the Google Voice account page failed for " + emailAddress + " with response " + response.StatusCode + "."); } else { Log_External(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, response.ResponseUri + " Google Voice home page loaded successfully.", m_username)); } StreamReader reader = new StreamReader(response.GetResponseStream()); string responseFromServer = reader.ReadToEnd(); response.Close(); // Extract the rnr field from the HTML. Match rnrMatch = Regex.Match(responseFromServer, @"name=""_rnr_se"".*?value=""(?<rnrvalue>.*?)"""); if (rnrMatch.Success) { return(rnrMatch.Result("${rnrvalue}")); } else { Log_External(new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.DialPlan, "Could not find _rnr_se key on your Google Voice account page, callback cannot proceed.", m_username)); throw new ApplicationException("Could not find _rnr_se key on your Google Voice account page, callback cannot proceed."); } } catch (Exception excp) { logger.Error("Exception GoogleVoiceCall Login. " + excp.Message); throw; } }
/// <summary> /// Begin processing a web page via an incoming stream. /// </summary> protected void PreProcess() { // stop the time keeper _time.Stop(); // add to the connection time if (OnConnect != null) { OnConnect.ArgA = _time.Milliseconds; OnConnect.Run(); } // continue processing Processing = true; Connecting = false; // has the request been aborted? if (_abort) { // yes, stop the request _request.Abort(); _response.Dispose(); // run on stop _stopped.Run(); return; } try { // add the response cookies Cookies.Add(_response.Cookies); } catch { // the cookies were invalid - init a new container Cookies = new CookieContainer(_session.CrawlerMaxCookieCount, _session.CrawlerMaxCookieCount, _session.CrawlerMaxCookieSize); } // get the stream _stream = _response.GetResponseStream(); // was the response stream retrieved? if (_stream == null) { // no, stop and dispose of the resources _request.Abort(); _response.Dispose(); Log.Debug("No stream retrieved from request to '" + Url + "'."); // callback if (OnAttempt != null) { OnAttempt.Run(); } // run on stop _stopped.Run(); return; } // get the encoding string encoder = _response.ContentType; _decoder = null; if (encoder != null && encoder.Length > 3) { int index = encoder.IndexOf("charset=", StringComparison.OrdinalIgnoreCase); if (index != -1 && index + 9 == encoder.Length) { encoder = encoder.Substring(index + 9, encoder.Length - index - 9); if (encoder.Length != 0) { int count; switch (encoder[0]) { case 'u': if (encoder.Length > 4) { switch (encoder[4]) { case '3': _decoder = Encoding.UTF32.GetDecoder(); // set the char buffer count = Encoding.UTF32.GetMaxCharCount(_session.CrawlerByteBuffer); if (_chars.Length != count) { _chars = new char[count]; } break; case '7': _decoder = Encoding.UTF7.GetDecoder(); // set the char buffer count = Encoding.UTF7.GetMaxCharCount(_session.CrawlerByteBuffer); if (_chars.Length != count) { _chars = new char[count]; } break; } } break; case 'a': _decoder = _ascii; // set the char buffer count = Encoding.ASCII.GetMaxCharCount(_session.CrawlerByteBuffer); if (_chars.Length != count) { _chars = new char[count]; } break; } } } } // is the decoder the default UTF8? if (_decoder == null) { // yes, set the decoder _decoder = _utf8; // set the char buffer int count = Encoding.UTF8.GetMaxCharCount(_session.CrawlerByteBuffer); if (_chars.Length != count) { _chars = new char[count]; } } // is the content encoded? if (!string.IsNullOrEmpty(_response.ContentEncoding)) { try { switch (_response.ContentEncoding.ToLowercase()) { case "gzip": _stream = new System.IO.Compression.GZipStream(_stream, System.IO.Compression.CompressionMode.Decompress, false); break; case "deflate": _stream = new System.IO.Compression.DeflateStream(_stream, System.IO.Compression.CompressionMode.Decompress, false); break; } } catch { // the base stream was unreadable - run on attempt if (OnAttempt != null) { OnAttempt.Run(); } // stop and dispose of the resources _request.Abort(); _response.Dispose(); // run on stopped _stopped.Run(); return; } } // has the request been aborted? if (_abort) { // yes, stop and dispose of the resources _request.Abort(); _response.Dispose(); // run on stopped _stopped.Run(); return; } // the number of bytes to read from the current url _byteCountMax = Randomize.Range(_session.CrawlerMinBytes, _session.CrawlerMaxBytes); _byteCount = 0; // start the timers _time.Start(); _timer.OnDone = _timeout; _timer.Reset(Randomize.Range(_session.CrawlerMinProcessTimeout, _session.CrawlerMaxProcessTimeout)); // start processing the stream _process.Run(); }
override protected async Task <WebClientByteResult> Run(WebRequest webRequest) { HttpResponseMessage response = null; var request = new HttpRequestMessage(); request.Headers.ExpectContinue = false; request.RequestUri = new Uri(webRequest.Url); if (webRequest.EmulateBrowser == true) { request.Headers.UserAgent.ParseAdd(BrowserUtil.ChromeUserAgent); } else { request.Headers.UserAgent.ParseAdd("Jackett/" + configService.GetVersion()); } // clear cookies from cookiecontainer var oldCookies = cookies.GetCookies(request.RequestUri); foreach (Cookie oldCookie in oldCookies) { oldCookie.Expired = true; } if (!string.IsNullOrEmpty(webRequest.Cookies)) { // add cookies to cookiecontainer var cookieUrl = new Uri(request.RequestUri.Scheme + "://" + request.RequestUri.Host); // don't include the path, Scheme is needed for mono compatibility foreach (var ccookiestr in webRequest.Cookies.Split(';')) { var cookiestrparts = ccookiestr.Split('='); var name = cookiestrparts[0].Trim(); if (string.IsNullOrWhiteSpace(name)) { continue; } var value = ""; if (cookiestrparts.Length >= 2) { value = cookiestrparts[1].Trim(); } var cookie = new Cookie(name, value); cookies.Add(cookieUrl, cookie); } } if (webRequest.Headers != null) { foreach (var header in webRequest.Headers) { if (header.Key != "Content-Type") { request.Headers.TryAddWithoutValidation(header.Key, header.Value); } } } if (!string.IsNullOrEmpty(webRequest.Referer)) { request.Headers.Referrer = new Uri(webRequest.Referer); } if (!string.IsNullOrEmpty(webRequest.RawBody)) { var type = webRequest.Headers.Where(h => h.Key == "Content-Type").Cast <KeyValuePair <string, string>?>().FirstOrDefault(); if (type.HasValue) { var str = new StringContent(webRequest.RawBody); str.Headers.Remove("Content-Type"); str.Headers.Add("Content-Type", type.Value.Value); request.Content = str; } else { request.Content = new StringContent(webRequest.RawBody); } request.Method = HttpMethod.Post; } else if (webRequest.Type == RequestType.POST) { if (webRequest.PostData != null) { request.Content = new FormUrlEncodedContent(webRequest.PostData); } request.Method = HttpMethod.Post; } else { request.Method = HttpMethod.Get; } response = await client.SendAsync(request); var result = new WebClientByteResult(); result.Content = await response.Content.ReadAsByteArrayAsync(); foreach (var header in response.Headers) { IEnumerable <string> value = header.Value; result.Headers[header.Key.ToLowerInvariant()] = value.ToArray(); } // some cloudflare clients are using a refresh header // Pull it out manually if (response.StatusCode == System.Net.HttpStatusCode.ServiceUnavailable && response.Headers.Contains("Refresh")) { var refreshHeaders = response.Headers.GetValues("Refresh"); var redirval = ""; var redirtime = 0; if (refreshHeaders != null) { foreach (var value in refreshHeaders) { var start = value.IndexOf("="); var end = value.IndexOf(";"); var len = value.Length; if (start > -1) { redirval = value.Substring(start + 1); result.RedirectingTo = redirval; // normally we don't want a serviceunavailable (503) to be a redirect, but that's the nature // of this cloudflare approach..don't want to alter BaseWebResult.IsRedirect because normally // it shoudln't include service unavailable..only if we have this redirect header. response.StatusCode = System.Net.HttpStatusCode.Redirect; redirtime = Int32.Parse(value.Substring(0, end)); System.Threading.Thread.Sleep(redirtime * 1000); } } } } if (response.Headers.Location != null) { result.RedirectingTo = response.Headers.Location.ToString(); } // Mono won't add the baseurl to relative redirects. // e.g. a "Location: /index.php" header will result in the Uri "file:///index.php" // See issue #1200 if (result.RedirectingTo != null && result.RedirectingTo.StartsWith("file://")) { var newRedirectingTo = result.RedirectingTo.Replace("file://", request.RequestUri.Scheme + "://" + request.RequestUri.Host); logger.Debug("[MONO relative redirect bug] Rewriting relative redirect URL from " + result.RedirectingTo + " to " + newRedirectingTo); result.RedirectingTo = newRedirectingTo; } result.Status = response.StatusCode; // Compatiblity issue between the cookie format and httpclient // Pull it out manually ignoring the expiry date then set it manually // http://stackoverflow.com/questions/14681144/httpclient-not-storing-cookies-in-cookiecontainer IEnumerable <string> cookieHeaders; var responseCookies = new List <Tuple <string, string> >(); if (response.Headers.TryGetValues("set-cookie", out cookieHeaders)) { foreach (var value in cookieHeaders) { logger.Debug(value); var nameSplit = value.IndexOf('='); if (nameSplit > -1) { responseCookies.Add(new Tuple <string, string>(value.Substring(0, nameSplit), value.Substring(0, value.IndexOf(';') == -1 ? value.Length : (value.IndexOf(';'))) + ";")); } } var cookieBuilder = new StringBuilder(); foreach (var cookieGroup in responseCookies.GroupBy(c => c.Item1)) { cookieBuilder.AppendFormat("{0} ", cookieGroup.Last().Item2); } result.Cookies = cookieBuilder.ToString().Trim(); } ServerUtil.ResureRedirectIsFullyQualified(webRequest, result); return(result); }
public static void Add_CookieCollection_Success() { CookieContainer cc1 = new CookieContainer(); CookieCollection cc2 = new CookieCollection(); cc2.Add(new Cookie("name1", "value", "", "contoso.com")); cc2.Add(new Cookie("name2", "value", "", "contoso.com")); cc1.Add(cc2); Assert.Equal(2, cc1.Count); }
public static void Add_SameCookieDifferentVairants_OverridesOlderVariant() { Uri uri = new Uri("http://domain.com"); Cookie c1 = new Cookie("name1", "value", "", ".domain.com"); // Variant = Plain Cookie c2 = new Cookie("name1", "value", "", ".domain.com") { Port = "\"80\"" }; // Variant = RFC2965 (should override) Cookie c3 = new Cookie("name1", "value", "", ".domain.com") { Port = "\"80, 90\"" }; // Variant = RFC2965 (should override) Cookie c4 = new Cookie("name1", "value", "", ".domain.com") { Version = 1 }; // Variant = RFC2109 (should be rejected) CookieContainer cc = new CookieContainer(); cc.Add(c1); // Adding a newer variant should override an older one cc.Add(c2); Assert.Equal(c2.Port, cc.GetCookies(uri)[0].Port); // Adding the same variant should override the existing one cc.Add(c3); Assert.Equal(c3.Port, cc.GetCookies(uri)[0].Port); // Adding an older variant shold be rejected cc.Add(c4); Assert.Equal(c3.Port, cc.GetCookies(uri)[0].Port); // Ensure that although we added 3 cookies, only 1 was actually added (the others were overriden or rejected) Assert.Equal(1, cc.Count); }
public static void Add_ExpiredCookie_NotAdded() { CookieContainer cc = new CookieContainer(); Cookie c1 = new Cookie("name1", "value", "", ".domain.com") { Expired = true }; Cookie c2 = new Cookie("name2", "value", "", ".domain.com"); cc.Add(c1); cc.Add(c2); // Ignores adding expired cookies Assert.Equal(1, cc.Count); Assert.Equal(c2, cc.GetCookies(new Uri("http://domain.com"))[0]); // Manually expire cookie c2.Expired = true; cc.Add(c2); Assert.Equal(0, cc.Count); }
public FotkiRemote () { //FIXME this api is lame cookies = new CookieContainer (); cookies.Add (new Cookie ("c", "1")); }
public string PostHtml() { Random Rdm = new Random(); //产生0到100的随机数 int iRdm = Rdm.Next(423456789, 499999999); string wxuin = iRdm.ToString(); string pass_ticket = "1KLZo5j/85JCXGrbyj5vH6Wn2Ek1qDqjqj2U5tik1232P47mLxmwM+avvXgfWjy5"; string appmsg_token = "954_1hcOzNorDwiokamoRrnyUm3rQ1fVwUQk-ZDN0s061MxSYtM-BH5313uQ0n5bDgdUat4FJSVA7RrhkCIN"; string postData = "action=vote&__biz=MzA4MDIzOTQ5OQ%3D%3D&uin=777&key=777&pass_ticket=" + pass_ticket + "&appmsg_token=" + appmsg_token + "&f=json&json=%7B%22super_vote_item%22%3A%5B%7B%22vote_id%22%3A495474521%2C%22item_idx_list%22%3A%7B%22item_idx%22%3A%5B%2216%22%5D%7D%7D%2C%7B%22vote_id%22%3A495474522%2C%22item_idx_list%22%3A%7B%22item_idx%22%3A%5B%2219%22%5D%7D%7D%5D%2C%22super_vote_id%22%3A495474497%7D&idx=2&mid=2653078119&wxtoken=777"; string cookieStr = "rewardsn=; wxuin=" + wxuin + "; devicetype=android-23; version=26060636; lang=zh_CN; pass_ticket=" + pass_ticket + "; wap_sid2=CLfb39UBElw3ZDlXaU5iNlVsYzB0UVlia3NvZktSWHpoM3FfVl9udFhBWlhJdlRrV0N4NVVwTUZ3V2ZCYW5aWUZrTkxMSVBZYlZyc2xUbTc0THZmWE16ZDNBWEkxYm9EQUFBfjCyyoTXBTgNQAE=; wxtokenkey=777"; string RefererURl = "https://mp.weixin.qq.com/mp/newappmsgvote?action=show&__biz=MzA4MDIzOTQ5OQ==&supervoteid=495474497&uin=777&key=777&pass_ticket=" + pass_ticket + "&wxtoken=777&mid=2653078119&idx=2&appmsg_token=" + appmsg_token; string URL = "https://mp.weixin.qq.com/mp/newappmsgvote"; string[] cookstr = cookieStr.Split(';'); foreach (string str in cookstr) { string[] cookieNameValue = str.Split('='); Cookie ck = new Cookie(cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().ToString()); ck.Domain = "mp.weixin.qq.com"; _cc.Add(ck); } ASCIIEncoding encoding = new ASCIIEncoding(); byte[] data = encoding.GetBytes(postData);//Encoding.UTF8.GetBytes(postData); HttpWebRequest httpWebRequest; HttpWebResponse webResponse; Stream getStream; StreamReader streamReader; httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL); httpWebRequest.Host = "mp.weixin.qq.com"; httpWebRequest.ContentLength = data.Length; httpWebRequest.Accept = "application/json"; httpWebRequest.Headers.Add("Origin", "https://mp.weixin.qq.com"); httpWebRequest.Headers.Add("X-Requested-With", "XMLHttpRequest"); httpWebRequest.UserAgent = "Mozilla/5.0 (Linux; Android 6.0; 1501-A02 Build/MRA58K; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/6.2 TBS/044028 Mobile Safari/537.36 MicroMessenger/6.6.6.1300(0x26060636) NetType/WIFI Language/zh_CN"; httpWebRequest.ContentType = "application/x-www-form-urlencoded"; httpWebRequest.Referer = RefererURl; httpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate"); httpWebRequest.Headers.Add("Accept-Language", "zh-CN,en-US;q=0.8"); httpWebRequest.Headers.Add("Q-UA2", "QV=3&PL=ADR&PR=WX&PP=com.tencent.mm&PPVN=6.6.6&TBSVC=43607&CO=BK&COVC=044028&PB=GE&VE=GA&DE=PHONE&CHID=0&LCID=9422&MO= 1501-A02 &RL=720*1280&OS=6.0&API=23"); httpWebRequest.Headers.Add("Q-GUID", "04dd8ef186bdc34bdedac5a413b788cb"); httpWebRequest.Headers.Add("Q-Auth", "31045b957cf33acf31e40be2f3e71c5217597676a9729f1b"); httpWebRequest.CookieContainer = _cc; httpWebRequest.Method = "POST"; // httpWebRequest.AllowAutoRedirect = true; Stream pReqStream = httpWebRequest.GetRequestStream(); // Send the data. pReqStream.Write(data, 0, data.Length); pReqStream.Close(); webResponse = (HttpWebResponse)httpWebRequest.GetResponse(); getStream = webResponse.GetResponseStream(); streamReader = new StreamReader(getStream, Encoding.UTF8); string getString = ""; getString = streamReader.ReadToEnd(); streamReader.Close(); getStream.Close(); webResponse.Close(); return(getString); }
public HttpWebResponse Submit(Uri uri, FSpot.ProgressItem progress_item) { this.Progress = progress_item; Request = (HttpWebRequest)WebRequest.Create(uri); CookieCollection cookie_collection = Cookies.GetCookies(uri); if (uri.UserInfo != null && uri.UserInfo != String.Empty) { NetworkCredential cred = new NetworkCredential(); cred.GetCredential(uri, "basic"); CredentialCache credcache = new CredentialCache(); credcache.Add(uri, "basic", cred); Request.PreAuthenticate = true; Request.Credentials = credcache; } Request.ServicePoint.Expect100Continue = expect_continue; Request.CookieContainer = new CookieContainer(); foreach (Cookie c in cookie_collection) { if (SuppressCookiePath) { Request.CookieContainer.Add(new Cookie(c.Name, c.Value)); } else { Request.CookieContainer.Add(c); } } Request.Method = "POST"; Request.Headers["Accept-Charset"] = "utf-8;"; //Request.UserAgent = "F-Spot Gallery Remote Client"; Request.UserAgent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7) Gecko/20040626 Firefox/0.9.1"; Request.Proxy = WebProxy.GetDefaultProxy(); if (multipart) { GenerateBoundary(); Request.ContentType = "multipart/form-data; boundary=" + boundary; Request.Timeout = Request.Timeout * 3; long length = 0; for (int i = 0; i < Items.Count; i++) { FormItem item = (FormItem)Items[i]; length += MultipartLength(item); } length += end_boundary.Length + 2; //Request.Headers["My-Content-Length"] = length.ToString (); if (Buffer == false) { Request.ContentLength = length; Request.AllowWriteStreamBuffering = false; } } else { Request.ContentType = "application/x-www-form-urlencoded"; } stream_writer = new StreamWriter(Request.GetRequestStream()); first_item = true; for (int i = 0; i < Items.Count; i++) { FormItem item = (FormItem)Items[i]; Write(item); } if (multipart) { stream_writer.Write(end_boundary + "\r\n"); } stream_writer.Flush(); stream_writer.Close(); HttpWebResponse response; try { response = (HttpWebResponse)Request.GetResponse(); //Console.WriteLine ("found {0} cookies", response.Cookies.Count); foreach (Cookie c in response.Cookies) { Cookies.Add(c); } } catch (WebException e) { if (e.Status == WebExceptionStatus.ProtocolError && ((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.ExpectationFailed && expect_continue) { e.Response.Close(); expect_continue = false; return(Submit(uri, progress_item)); } throw new WebException(Mono.Unix.Catalog.GetString("Unhandled exception"), e); } return(response); }
public static void AddCookieCollection_Null_Throws() { CookieContainer cc = new CookieContainer(); CookieCollection cookieCollection = null; Assert.Throws<ArgumentNullException>(() => cc.Add(cookieCollection)); }
/// <summary> /// Logs the user in /// </summary> /// <param name="user">Reddit account username</param> /// <param name="pswd">Reddit account password</param> /// <returns>True/False depending on success of login</returns> public bool Login(string user, string pswd) { string postData = string.Format("api_type=json&user={0}&passwd={1}", user, pswd); string loginURI = m_domain + string.Format(APIPaths.login, user); Hashtable response = SendPOST(postData, loginURI); m_usr = user; m_errors = GetErrorsFromRedditJson(response); //First check for errors. Should always contain errors key. if (m_errors != "" ) { return false; } //Only need the data segment Hashtable data = ((Hashtable)((Hashtable)response["json"])["data"]); m_modhash = data["modhash"].ToString(); string cookieval = data["cookie"].ToString(); redditCookie = new CookieContainer(); Uri cookieuri = new Uri(m_domain + string.Format(APIPaths.login, user)); redditCookie.Add(cookieuri, new Cookie("reddit_session", cookieval.Replace(",", "%2c").Replace(":", "%3A"), "/", "reddit.com")); jsonGet.Headers["cookie"] = redditCookie.GetCookieHeader(cookieuri); jsonGet.Headers["Useragent"] = m_useragent; m_logged_in = true; return true; }
public void AddCookie(string Name, string Value, string Domain) { Cookies.Add(new Cookie(Name, Value, String.Empty, Domain)); }
public static void Add_NullCookieCollection_Throws() { CookieContainer cc = new CookieContainer(); Assert.Throws<ArgumentNullException>(() => cc.Add((CookieCollection)null)); // Null cookie }
public static void Add_ReachedMaxCount_NotAdded() { CookieContainer cc = new CookieContainer(4); for (int i = 1; i <= 4; i++) { cc.Add(new Cookie("name" + i.ToString(), "value", "", ".domain.com")); } Assert.Equal(4, cc.Count); cc.Add(new Cookie("name5", "value", "", ".domain.com")); Assert.Equal(4, cc.Count); }
public static void Add_Cookie_Success() { Cookie c1 = new Cookie("name1", "value", "", "contoso.com"); Cookie c2 = new Cookie("name2", "", "", "contoso.com") { Secure = true }; Cookie c3 = new Cookie("name3", "value", "", "contoso.com") { Port = "\"80, 90, 100\"" }; Cookie c4 = new Cookie("name4", "value", "", ".contoso.com"); Cookie c5 = new Cookie("name5", "value", "", "127.0.0.1"); CookieContainer cc1 = new CookieContainer(); Assert.Equal(0, cc1.Count); cc1.Add(c1); cc1.Add(c2); cc1.Add(c3); cc1.Add(c4); cc1.Add(c5); Assert.Equal(5, cc1.Count); }
private void getVMFiles(String[] args) { _service = cb.getConnection()._service; _sic = cb.getConnection()._sic; String vmName = cb.get_option("vmname"); String localpath = cb.get_option("localpath"); Hashtable downloadedDir = new Hashtable(); try { Cookie cookie = cb._connection._service.CookieContainer.GetCookies( new Uri(cb.get_option("url")))[0]; CookieContainer cookieContainer = new CookieContainer(); cookieContainer.Add(cookie); ManagedObjectReference vmmor = null; vmmor = cb.getServiceUtil().GetDecendentMoRef(null, "VirtualMachine", vmName); if (vmmor != null) { String dataCenterName = getDataCenter(vmmor); String[] vmDirectory = getVmDirectory(vmmor); if (vmDirectory[0] != null) { Console.WriteLine("Downloading Virtual Machine Configuration Directory"); String dataStoreName = vmDirectory[0].Substring(vmDirectory[0].IndexOf("[") + 1, vmDirectory[0].LastIndexOf("]") - 1); int length = vmDirectory[0].LastIndexOf("/") - vmDirectory[0].IndexOf("]") - 2; String configurationDir = vmDirectory[0].Substring(vmDirectory[0].IndexOf("]") + 2, length); String localDirPath = cb.get_option("localpath") + "/" + configurationDir + "#vm#" + dataStoreName; Directory.CreateDirectory(localDirPath); downloadDirectory(configurationDir, localDirPath, dataStoreName, dataCenterName); downloadedDir.Add(configurationDir + "#vm#" + dataStoreName, "Directory"); Console.WriteLine("Downloading Virtual Machine" + " Configuration Directory Complete"); } if (vmDirectory[1] != null) { Console.WriteLine("Downloading Virtual Machine Snapshot / Suspend / Log Directory"); for (int i = 1; i < vmDirectory.Length; i++) { String dataStoreName = vmDirectory[i].Substring(vmDirectory[i].IndexOf("[") + 1, vmDirectory[i].LastIndexOf("]") - 1); String configurationDir = ""; ServiceContent sc = cb.getConnection().ServiceContent; String apiType = sc.about.apiType; if (apiType.Equals("VirtualCenter")) { configurationDir = vmDirectory[i].Substring(vmDirectory[i].IndexOf("]") + 2); configurationDir = configurationDir.Substring(0, configurationDir.Length - 1); } else { configurationDir = vmDirectory[i].Substring(vmDirectory[i].IndexOf("]") + 2); } if (!downloadedDir.ContainsKey(configurationDir + "#vm#" + dataStoreName)) { String localDirPath = cb.get_option("localpath") + "/" + configurationDir + "#vm#" + dataStoreName; Directory.CreateDirectory(localDirPath); downloadDirectory(configurationDir, localDirPath, dataStoreName, dataCenterName); downloadedDir.Add(configurationDir + "#vm#" + dataStoreName, "Directory"); } else { Console.WriteLine("Already Downloaded"); } } Console.WriteLine("Downloading Virtual Machine Snapshot" + " / Suspend / Log Directory Complete"); } String [] virtualDiskLocations = getVDiskLocations(vmmor); if (virtualDiskLocations != null) { Console.WriteLine("Downloading Virtual Disks"); for (int i = 0; i < virtualDiskLocations.Length; i++) { if (virtualDiskLocations[i] != null) { String dataStoreName = virtualDiskLocations[i].Substring( virtualDiskLocations[i].IndexOf("[") + 1, virtualDiskLocations[i].LastIndexOf("]") - 1); String configurationDir = virtualDiskLocations[i].Substring( virtualDiskLocations[i].IndexOf("]") + 2, virtualDiskLocations[i].LastIndexOf("/") - virtualDiskLocations[i].IndexOf("]") - 2); if (!downloadedDir.ContainsKey(configurationDir + "#vdisk#" + dataStoreName)) { String localDirPath = cb.get_option("localpath") + "/" + configurationDir + "#vdisk#" + dataStoreName; Directory.CreateDirectory(localDirPath); downloadDirectory(configurationDir, localDirPath, dataStoreName, dataCenterName); downloadedDir.Add(configurationDir + "#vdisk#" + dataStoreName, "Directory"); } else { Console.WriteLine("Already Downloaded"); } } else { // Do Nothing } } Console.WriteLine("Downloading Virtual Disks Complete"); } else { // Do Nothing } } else { Console.WriteLine("Virtual Machine " + cb.get_option("vmname") + " Not Found."); } } catch (Exception e) { cb.log.LogLine("GetVMFiles failed for VM : " + vmName); throw e; } }
public static void Add_VerificationFailedCookie_Throws() { CookieContainer cc = new CookieContainer(); foreach (Cookie c in CookieTest.InvalidCookies()) { Assert.Throws<CookieException>(() => cc.Add(c)); } }
public static void GetCookies_DifferentPaths_GetsMatchedPathsIncludingEmptyPath() { // Internally, paths are stored alphabetically sorted - so expect a cookie collection sorted by the path of each cookie // This test ensures that all cookies with paths (that the path specified by the uri starts with) are returned Cookie c1 = new Cookie("name1", "value", "/aa", ".url.com"); Cookie c2 = new Cookie("name2", "value", "/a", ".url.com"); Cookie c3 = new Cookie("name3", "value", "/b", ".url.com"); // Should be ignored - no match with the URL's path Cookie c4 = new Cookie("name4", "value", "/", ".url.com"); // Should NOT be ignored (has no path specified) CookieContainer cc1 = new CookieContainer(); cc1.Add(c1); cc1.Add(c2); cc1.Add(c3); cc1.Add(c4); CookieCollection cc2 = cc1.GetCookies(new Uri("http://url.com/aaa")); Assert.Equal(3, cc2.Count); Assert.Equal(c1, cc2[0]); Assert.Equal(c2, cc2[1]); Assert.Equal(c4, cc2[2]); }
/// <summary> /// Sets and associates a specified CookieCollection with a URL in the wrapper's CookieCollection. /// </summary> /// <param name="container">Required. The jar of cookies to operate on.</param> /// <param name="url">Required. The URL to associate the CookieCollection with.</param> /// <param name="cookieCollection">Required. The CookieCollection to add to the wrapper's CookieCollection.</param> public static void Add(this CookieContainer container, string url, CookieCollection cookieCollection) { try { container.Add(new Uri(url), cookieCollection); } catch { } }
//获取联系人 private List <Dictionary <string, dynamic> > webwxgetcontact() { var url = base_url + String.Format("webwxgetcontact?pass_ticket={0}&skey={1}&r={2}", pass_ticket, skey, get_timestamp()); var http = WebRequest.Create(url) as HttpWebRequest; #if (DEBUG) Uri target = new Uri("https://wx.qq.com"); cookieContainer.Add(new Cookie("mm_lang", "zh_CN") { Domain = target.Host }); cookieContainer.Add(new Cookie("webwx_data_ticket", "AQcsqhDcQuYzt1F1QvsvBV9W") { Domain = target.Host }); cookieContainer.Add(new Cookie("wxuin", "2622149902") { Domain = target.Host }); cookieContainer.Add(new Cookie("wxsid", "ZHu/WOz9i7GZXcFN") { Domain = target.Host }); cookieContainer.Add(new Cookie("wxloadtime", "1452642551") { Domain = target.Host }); cookieContainer.Add(new Cookie("webwxuvid", "20e90937e5f3173d94c75ee2c3805a26a0da27cb6eb2a7f692f9b26c09eb4a2d68ac2e5055bac129bc2e653ddc9b3312") { Domain = target.Host }); #endif http.CookieContainer = cookieContainer; http.ContentType = "application/json; charset=UTF-8"; http.Method = "GET"; var response = http.GetResponse() as HttpWebResponse; var dic = deserilizeJson(response); List <Dictionary <string, dynamic> > memberlist = new List <Dictionary <string, dynamic> >(dic["MemberList"]. ToArray(typeof(Dictionary <string, dynamic>))); var special_users = new List <string>() { "newsapp", "fmessage", "filehelper", "weibo", "qqmail", "tmessage", "qmessage", "qqsync", "floatbottle", "lbsapp", "shakeapp", "medianote", "qqfriend", "readerapp", "blogapp", "facebookapp", "masssendapp", "meishiapp", "feedsapp", "voip", "blogappweixin", "weixin", "brandsessionholder", "weixinreminder", "wxid_novlwrv3lqwv11", "gh_22b87fa7cb3c", "officialaccounts", "notification_messages", "wxitil", "userexperience_alarm" }; for (int i = memberlist.Count - 1; i > -1; i--) { Dictionary <string, dynamic> member = memberlist[i]; if ((8 & Convert.ToInt32(member["VerifyFlag"])) != 0) { memberlist.Remove(member); } else if (special_users.Contains(member["UserName"])) { memberlist.Remove(member); } else if (member["UserName"].Contains("@@")) { memberlist.Remove(member); } else if (member["UserName"] == self["UserName"]) { memberlist.Remove(member); } } return(memberlist); }
public string GetFin(string viewnumber = "") { //string test = "yes"; var fname = "f" + viewnumber + "-" + (DateTime.Now.ToString(@"yyyy-MM-dd")); var path = System.Web.Hosting.HostingEnvironment.MapPath(string.Format(@"~/App_Data/{0}.csv", fname)); string url = @"http://finviz.com/login_submit.ashx"; string urlexport = String.Format(@"http://elite.finviz.com/export.ashx?v={0}&f=sh_avgvol_o500,sh_instown_o70,sh_price_o5,ta_rsi_os40,targetprice_a20&ft=3&o=sector", viewnumber); //@"http://elite.finviz.com/export.ashx?v=171&f=sh_avgvol_o500,sh_instown_o70,sh_price_o5,ta_rsi_os40,targetprice_a20&ft=3&o=sector"; //@"http://elite.finviz.com/export.ashx?v=150&f=sh_avgvol_o500,sh_instown_o70,sh_price_o5,ta_rsi_os40,targetprice_a20&ft=3&o=sector"; // http://elite.finviz.com/screener.ashx?v=152&f=sh_avgvol_o500,sh_instown_o70,sh_price_o5,ta_rsi_os40,targetprice_a20&ft=4&o=-dividendyield CookieContainer cc = new CookieContainer(); HttpWebRequest http = WebRequest.Create(url) as HttpWebRequest; http.KeepAlive = true; http.Method = "POST"; http.ContentType = "application/x-www-form-urlencoded"; // Uri target = new Uri("http://finviz.com/"); string cols = HttpContext.Current.Server.UrlEncode("0,1,2,3,4,5,14,28,44,46,52,54,57,58,59,65,66,67,69"); string f = HttpContext.Current.Server.UrlEncode("GA1.2.1218077229.1483581984"); string g = HttpContext.Current.Server.UrlEncode("screener.ashx?v=151&f=sh_avgvol_o500,sh_instown_o70,sh_price_o5,ta_rsi_os40,targetprice_a20&ft=3&o=sector"); string h = HttpContext.Current.Server.UrlEncode("1"); //http://elite.finviz.com/export.ashx?v=151&f=sh_avgvol_o500,sh_instown_o70,sh_price_o5,ta_rsi_os40,targetprice_a20&ft=3&o=sector cc.Add(new Cookie("customTable", cols) { Domain = target.Host }); //_ga=GA1.2.1218077229.1483581984 cc.Add(new Cookie("screenerUrl", g) { Domain = target.Host }); //screener.ashx?v=151&f=sh_avgvol_o500,sh_instown_o70,sh_price_o5,ta_rsi_os40,targetprice_a20&ft=3&o=sector //cc.Add(new Cookie("_ga", f) { Domain = target.Host }); //cc.Add(new Cookie("_gat", h) { Domain = target.Host }); cc.Add(new Cookie("domain", ".finviz.com") { Domain = target.Host }); //domain=.finviz.com //_gat=1 http.CookieContainer = cc; string postData = @"email=mattdunndc%40gmail.com&password=Be0nthew%40tch&remember=true"; byte[] dataBytes = Encoding.UTF8.GetBytes(postData); http.ContentLength = dataBytes.Length; using (Stream postStream = http.GetRequestStream()) { postStream.Write(dataBytes, 0, dataBytes.Length); } HttpWebResponse httpResponse = http.GetResponse() as HttpWebResponse; // Probably want to inspect the http.Headers here first http = WebRequest.Create(urlexport) as HttpWebRequest; // http.CookieContainer = cc; HttpWebResponse httpResponse2 = http.GetResponse() as HttpWebResponse; byte[] data = new System.IO.BinaryReader(httpResponse2.GetResponseStream()).ReadBytes((int)httpResponse2.ContentLength); System.IO.File.WriteAllBytes(path, data); return(httpResponse2.StatusDescription); }
private async void _ButtonSubmit_Clicked(object sender, EventArgs e) { if (string.IsNullOrEmpty(_EntryKullaniciAdi.Text) || string.IsNullOrEmpty(_EntrySifre.Text)) { await DisplayAlert("Uyarı", "Gerekli bilgileri eksiksiz doldurunuz!", "Ok"); return; } var selectedIp = _PickerIp.SelectedItem.ToString(); if (string.IsNullOrEmpty(selectedIp)) { selectedIp = "http://192.168.1.1/"; } else { selectedIp = "http://" + selectedIp.Trim() + "/"; } HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create($"{selectedIp}login/login-page.cgi"); request.Method = "POST"; request.ContentType = "text/html"; request.CookieContainer = new CookieContainer(); request.UserAgent = "-"; var postdata = $"AuthName={_EntryKullaniciAdi.Text.Trim()}" + $"&AuthPassword={_EntrySifre.Text.Trim()}"; var data = Encoding.UTF8.GetBytes(postdata); request.ContentLength = data.Length; var stream = request.GetRequestStream(); stream.Write(data, 0, data.Length); var response = (HttpWebResponse)request.GetResponse(); var source = new StreamReader(response.GetResponseStream()).ReadToEnd(); CookieContainer cookieContainer = new CookieContainer(); foreach (Cookie item in response.Cookies) { cookieContainer.Add(item); } if (source.IndexOf("top.location=") != -1) { //bağlantı başarılı //await Loading("İşlem gerçekleştiriliyor..."); Task t1 = Task.Run(() => { using (UserDialogs.Instance.Loading("İşlem gerçekleştiriliyor...", null, null, true, MaskType.Black)) { //Task.Delay(TimeSpan.FromMilliseconds(30000)); Thread.Sleep(30000); } }); Task t2 = Task.Run(() => { HttpWebRequest request2 = (HttpWebRequest)HttpWebRequest.Create($"{selectedIp}pages/tabFW/reboot-rebootpost.cgi"); request2.Method = "GET"; request2.ContentType = "text/html"; request2.CookieContainer = cookieContainer; var response2 = (HttpWebResponse)request2.GetResponse(); source = new StreamReader(response2.GetResponseStream()).ReadToEnd(); }); } else { await DisplayAlert("Başarısız İşlem", "Modem ile bağlantı kurulamadı.Bilgileri kontrol ediniz", "Ok"); } }
protected override void AddProtocolSpecificCookiesToServerRequest(CookieContainer cookieContainer) { cookieContainer.Add(this.proxyLogonResponseCookies); }
/// <summary> /// 设置Cookie /// </summary> /// <param name="cookie">cookie</param> public TRequest Cookie(Cookie cookie) { _cookieContainer.Add(new Uri(_url), cookie); return(This()); }
public override void Login(string Username, string Password, string twofa) { HttpWebRequest getHeaders = HttpWebRequest.Create("https://betking.io/bitcoindice#dice") as HttpWebRequest; if (Prox != null) { getHeaders.Proxy = Prox; } var cookies = new CookieContainer(); getHeaders.CookieContainer = cookies; HttpWebResponse Response = null; string rqtoken = ""; string s1 = ""; try { Response = (HttpWebResponse)getHeaders.GetResponse(); s1 = new StreamReader(Response.GetResponseStream()).ReadToEnd(); string tmp = s1.Substring(s1.IndexOf("__RequestVerificationToken") + "__RequestVerificationToken\" type=\"hidden\" value=\"".Length); s = rqtoken = tmp.Substring(0, tmp.IndexOf("\"")); } catch (WebException e) { finishedlogin(false); return; } getHeaders = HttpWebRequest.Create("https://betking.io/account/login") as HttpWebRequest; if (Prox != null) { getHeaders.Proxy = Prox; } getHeaders.CookieContainer = new CookieContainer(); foreach (Cookie c in Response.Cookies) { getHeaders.CookieContainer.Add(c); } getHeaders.Method = "POST"; string post = string.Format("userName={0}&password={1}&twoFactorCode={2}&__RequestVerificationToken={3}", Username, Password, twofa, rqtoken); getHeaders.ContentType = "application/x-www-form-urlencoded"; getHeaders.ContentLength = post.Length; using (var writer = new StreamWriter(getHeaders.GetRequestStream())) { string writestring = post as string; writer.Write(writestring); } try { Response = (HttpWebResponse)getHeaders.GetResponse(); s1 = new StreamReader(Response.GetResponseStream()).ReadToEnd(); if (!s1.ToLower().Contains("true")) { finishedlogin(false); return; } /*string tmp = s1.Substring(s1.IndexOf("__RequestVerificationToken") + "__RequestVerificationToken\" type=\"hidden\" value=\"".Length); * rqtoken = tmp.Substring(0, tmp.IndexOf("\""));*/ } catch (WebException e) { Response = (HttpWebResponse)e.Response; s1 = new StreamReader(Response.GetResponseStream()).ReadToEnd(); finishedlogin(false); return; } foreach (Cookie c in Response.Cookies) { if (c.Name == "__RequestVerificationToken") { rqtoken = c.Value; } Cookies.Add(c); } Cookies.Add((new Cookie("PRC_Affiliate", "357", "/", "betking.io"))); con.CookieContainer = Cookies; try { getHeaders = HttpWebRequest.Create("https://betking.io/bitcoindice#dice") as HttpWebRequest; if (Prox != null) { getHeaders.Proxy = Prox; } getHeaders.CookieContainer = Cookies; Response = (HttpWebResponse)getHeaders.GetResponse(); string stmp = new StreamReader(Response.GetResponseStream()).ReadToEnd(); string sstmp = stmp.Substring(stmp.IndexOf("__RequestVerificationToken") + "__RequestVerificationToken\" type=\"hidden\" value=\"".Length); s = rqtoken = sstmp.Substring(0, sstmp.IndexOf("\"")); dicehub = con.CreateHubProxy("diceHub"); con.Start().Wait(); dicehub.Invoke("joinChatRoom", 1); dicehub.On <string, string, string, int, int, bool>("chat", ReceivedChat); dicehub.On <string, string, string, int, bool>("receivePrivateMesssage", ReceivedChat); dicehub.On <PRCMYstats>("diceBetResult", DiceBetResult); dicehub.On <PRCResetSeed>("generateServerSeedResult", GenerateServerSeedResult); dicehub.On <PRCResetSeed>("saveClientSeedResult", SaveClientSeedResult); getHeaders = HttpWebRequest.Create("https://betking.io/account/GetUserAccount") as HttpWebRequest; if (Prox != null) { getHeaders.Proxy = Prox; } getHeaders.CookieContainer = Cookies; Response = (HttpWebResponse)getHeaders.GetResponse(); s1 = new StreamReader(Response.GetResponseStream()).ReadToEnd(); PRCUser tmp = json.JsonDeserialize <PRCUser>(s1); balance = (decimal)tmp.AvailableBalance; profit = (decimal)tmp.Profit; wagered = (decimal)tmp.Wagered; bets = (int)tmp.NumBets; wins = (int)tmp.Wins; losses = (int)tmp.Losses; UserID = tmp.Id; Parent.updateBalance((decimal)(balance)); Parent.updateBets(tmp.NumBets); Parent.updateLosses(tmp.Losses); Parent.updateProfit(profit); Parent.updateWagered(wagered); Parent.updateWins(tmp.Wins); //Parent.updateDeposit(tmp.DepositAddress); getHeaders = HttpWebRequest.Create("https://betking.io/account/GetCurrentSeed?gameType=0") as HttpWebRequest; if (Prox != null) { getHeaders.Proxy = Prox; } getHeaders.CookieContainer = Cookies; Response = (HttpWebResponse)getHeaders.GetResponse(); s1 = new StreamReader(Response.GetResponseStream()).ReadToEnd(); prcSeed getseed = json.JsonDeserialize <prcSeed>(s1); client = getseed.ClientSeed; serverhash = getseed.ServerHash; /*try * { * getHeaders = HttpWebRequest.Create("https://betking.io/account/getDepositAddress") as HttpWebRequest; * if (Prox != null) * getHeaders.Proxy = Prox; * getHeaders.CookieContainer = Cookies; * Response = (HttpWebResponse)getHeaders.GetResponse(); * s1 = new StreamReader(Response.GetResponseStream()).ReadToEnd(); * PRCDepost dep = json.JsonDeserialize<PRCDepost>(s1); * Parent.updateDeposit(dep.Address); * } * catch * { * new System.Threading.Thread(GetDeposit).Start(); * }*/ finishedlogin(true); return; } catch { finishedlogin(false); return; } finishedlogin(false); }
protected virtual async Task SendRequest() { await Task.Delay(1); rtxtLogs.Clear(); var request = new Httpdoer(txtURL.Text) { UseCookies = true, Timeout = TimeSpan.FromSeconds(decimal.ToDouble(numTimeout.Value)), Proxy = null, }; request.Trackings.Add(new BuibuiTracking()); //设置Cookie if (ckbKeepCookie.Checked) { request.Cookies = _cookies; } request.Headers.KeepAlive = ckbKeepAlive.Checked; request.Logger.Listeners.Add(new BuibuiListener(rtxtLogs)); request.HttpMethod = cbbHttpMethod.Text; if (!string.IsNullOrWhiteSpace(cbbContentType.Text)) { request.Body.ContentType = cbbContentType.Text; } //写入参数 WriteParams(request); //var response = request.Send(); var response = await request.SendAsync(); //返回正文 rtxtResponseBody.Tag = response?.Body?.ResponseBody; //设置编码 var encoding = response?.Body?.ContentType.Charset?.WebName; if (encoding == cbbEncoding.Text) { cbbEncoding_SelectedIndexChanged(cbbEncoding, EventArgs.Empty); } else { cbbEncoding.Text = response?.Body?.ContentType.Charset?.WebName; } txtRequestRaw.Text = response?.RequestData.Raw; txtResponseRaw.Text = response?.ResponseRaw; //显示视图 if (response?.Body?.ContentType.Format?.ToLowerInvariant() == "json") { ShowJson(); } else { ShowView(); } //返回头 ShowResponseHeaders(response); //显示Cookie ShowResponseCookie(response); if (response.Cookies != null) { _cookies.Add(response.Cookies); } if (response.Exception != null) { request.Error(response.Exception); SaveHistory(); throw new NotImplementedException(response.Exception.Message, response.Exception); } SaveHistory(); }
private async static Task <string> GetBearerTokenForScope(string email, string password, string targetscope, string clientId = "ms-app://s-1-15-2-1929064262-2866240470-255121345-2806524548-501211612-2892859406-1685495620/") { string retVal = string.Empty; email = WebUtility.UrlEncode(email); password = WebUtility.UrlEncode(password); targetscope = WebUtility.UrlEncode(targetscope); clientId = WebUtility.UrlEncode(clientId); HttpWebRequest hwreq = (HttpWebRequest)WebRequest.Create($"https://login.live.com/oauth20_authorize.srf?client_id={clientId}&scope={targetscope}&response_type=token&redirect_uri=https%3A%2F%2Flogin.live.com%2Foauth20_desktop.srf"); string MSPOK = string.Empty; string urlPost; string PPFT; try { HttpWebResponse hwresp = (HttpWebResponse)(await hwreq.GetResponseAsync()); foreach (string oCookie in hwresp.Headers["Set-Cookie"].Split(',')) { if (oCookie.Trim().StartsWith("MSPOK")) { MSPOK = oCookie.Trim().Substring(6, oCookie.IndexOf(';') - 6); MSPOK = WebUtility.UrlEncode(MSPOK); break; } } string responsePlain = string.Empty; using (var reader = new StreamReader(hwresp.GetResponseStream(), Encoding.UTF8)) { responsePlain = reader.ReadToEnd(); } PPFT = responsePlain.Substring(responsePlain.IndexOf("name=\"PPFT\"")); PPFT = PPFT.Substring(PPFT.IndexOf("value=") + 7); PPFT = PPFT.Substring(0, PPFT.IndexOf('\"')); urlPost = responsePlain.Substring(responsePlain.IndexOf("urlPost:") + 9); urlPost = urlPost.Substring(0, urlPost.IndexOf('\'')); } catch { return(string.Empty); } HttpClientHandler httpClientHandler = new HttpClientHandler { AllowAutoRedirect = false }; CookieContainer hwreqCC = new CookieContainer(); hwreqCC.Add(new Uri("https://login.live.com"), new Cookie("MSPOK", MSPOK) { Domain = "login.live.com" }); httpClientHandler.CookieContainer = hwreqCC; var client = new HttpClient(httpClientHandler); StringContent queryString = new StringContent($"login={email}&passwd={password}&PPFT={PPFT}", Encoding.UTF8); queryString.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); byte[] POSTByteArray = Encoding.UTF8.GetBytes($"login={email}&passwd={password}&PPFT={PPFT}"); queryString.Headers.ContentLength = POSTByteArray.Length; var hwresp2 = await client.PostAsync(new Uri(urlPost), queryString); try { foreach (string oLocationBit in hwresp2.Headers.Location.AbsoluteUri.Split('&')) { if (oLocationBit.Contains("access_token")) { retVal = oLocationBit.Substring(oLocationBit.IndexOf("access_token") + 13); if (retVal.Contains("&")) { retVal = retVal.Substring(0, retVal.IndexOf('&')); } break; } } } catch { return(string.Empty); } return(WebUtility.UrlDecode(retVal)); }
static bool PromptAndLogIn(CookieAwareWebClient wc, string tokenPath) { if (File.Exists(tokenPath)) { Console.Error.WriteLine("Found existing tokens, using them."); Uri desuraUri = new Uri("http://www.desura.com"); CookieContainer container = wc.CookieContainer; using (StreamReader sr = File.OpenText(tokenPath)) { while (!sr.EndOfStream) { string[] line = sr.ReadLine().Split('='); container.Add(desuraUri, new Cookie(line [0], line [1])); } } } else { Console.WriteLine("Log in"); Console.Write("Username/email address: "); string username = Console.ReadLine(); Console.Write("Password: "******"Empty email address"); return(false); } if (pass.Length == 0) { Console.WriteLine("Empty password"); return(false); } Console.WriteLine("Logging in..."); // Make args var form = System.Web.HttpUtility.ParseQueryString(string.Empty); form ["referer"] = "http://www.desura.com"; form ["username"] = username; form ["password"] = pass; form ["rememberme"] = "1"; form ["members"] = "Sign In"; wc.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded"); string result = wc.UploadString("https://secure.desura.com/members/login", form.ToString()); if (result.Contains("Login to Desura and enter a world of games to play, friends to challenge and community groups to join.")) { Console.Error.WriteLine("Login failed."); return(false); } Console.Error.WriteLine("Saving tokens..."); try { using (StreamWriter sw = File.CreateText(tokenPath)) { foreach (Cookie cookie in wc.CookieContainer.GetCookies(new Uri("http://www.desura.com"))) { sw.WriteLine(cookie.ToString()); } sw.Flush(); } } catch (Exception ex) { Console.Error.WriteLine("Error saving tokens ({0}), carrying on", ex.Message); } } return(true); }
/// <summary> /// Executes the login by using the Steam Website. /// This Method is not used by Steambot repository, but it could be very helpful if you want to build a own Steambot or want to login into steam services like backpack.tf/csgolounge.com. /// Updated: 10-02-2015. /// </summary> /// <param name="username">Your Steam username.</param> /// <param name="password">Your Steam password.</param> /// <returns>A bool containing a value, if the login was successful.</returns> public bool DoLogin(string username, string password) { var data = new NameValueCollection { { "username", username } }; // First get the RSA key with which we will encrypt our password. string response = Fetch("https://steamcommunity.com/login/getrsakey", "POST", data, false); GetRsaKey rsaJson = JsonConvert.DeserializeObject <GetRsaKey>(response); // Validate, if we could get the rsa key. if (!rsaJson.success) { return(false); } // RSA Encryption. RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); RSAParameters rsaParameters = new RSAParameters { Exponent = HexToByte(rsaJson.publickey_exp), Modulus = HexToByte(rsaJson.publickey_mod) }; rsa.ImportParameters(rsaParameters); // Encrypt the password and convert it. byte[] bytePassword = Encoding.ASCII.GetBytes(password); byte[] encodedPassword = rsa.Encrypt(bytePassword, false); string encryptedBase64Password = Convert.ToBase64String(encodedPassword); SteamResult loginJson = null; CookieCollection cookieCollection; string steamGuardText = ""; string steamGuardId = ""; // Do this while we need a captcha or need email authentification. Probably you have misstyped the captcha or the SteamGaurd code if this comes multiple times. do { Console.WriteLine("SteamWeb: Logging In..."); bool captcha = loginJson != null && loginJson.captcha_needed; bool steamGuard = loginJson != null && loginJson.emailauth_needed; string time = Uri.EscapeDataString(rsaJson.timestamp); string capGid = string.Empty; // Response does not need to send if captcha is needed or not. // ReSharper disable once MergeSequentialChecks if (loginJson != null && loginJson.captcha_gid != null) { capGid = Uri.EscapeDataString(loginJson.captcha_gid); } data = new NameValueCollection { { "password", encryptedBase64Password }, { "username", username } }; // Captcha Check. string capText = ""; if (captcha) { Console.WriteLine("SteamWeb: Captcha is needed."); System.Diagnostics.Process.Start("https://steamcommunity.com/public/captcha.php?gid=" + loginJson.captcha_gid); Console.WriteLine("SteamWeb: Type the captcha:"); string consoleText = Console.ReadLine(); if (!string.IsNullOrEmpty(consoleText)) { capText = Uri.EscapeDataString(consoleText); } } data.Add("captchagid", captcha ? capGid : ""); data.Add("captcha_text", captcha ? capText : ""); // Captcha end. // Added Header for two factor code. data.Add("twofactorcode", ""); // Added Header for remember login. It can also set to true. data.Add("remember_login", "false"); // SteamGuard check. If SteamGuard is enabled you need to enter it. Care probably you need to wait 7 days to trade. // For further information about SteamGuard see: https://support.steampowered.com/kb_article.php?ref=4020-ALZM-5519&l=english. if (steamGuard) { Console.WriteLine("SteamWeb: SteamGuard is needed."); Console.WriteLine("SteamWeb: Type the code:"); string consoleText = Console.ReadLine(); if (!string.IsNullOrEmpty(consoleText)) { steamGuardText = Uri.EscapeDataString(consoleText); } steamGuardId = loginJson.emailsteamid; // Adding the machine name to the NameValueCollection, because it is requested by steam. Console.WriteLine("SteamWeb: Type your machine name:"); consoleText = Console.ReadLine(); var machineName = string.IsNullOrEmpty(consoleText) ? "" : Uri.EscapeDataString(consoleText); data.Add("loginfriendlyname", machineName != "" ? machineName : "defaultSteamBotMachine"); } data.Add("emailauth", steamGuardText); data.Add("emailsteamid", steamGuardId); // SteamGuard end. // Added unixTimestamp. It is included in the request normally. var unixTimestamp = (int)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; // Added three "0"'s because Steam has a weird unix timestamp interpretation. data.Add("donotcache", unixTimestamp + "000"); data.Add("rsatimestamp", time); // Sending the actual login. using (HttpWebResponse webResponse = Request("https://steamcommunity.com/login/dologin/", "POST", data, false)) { var stream = webResponse.GetResponseStream(); if (stream == null) { return(false); } using (StreamReader reader = new StreamReader(stream)) { string json = reader.ReadToEnd(); loginJson = JsonConvert.DeserializeObject <SteamResult>(json); cookieCollection = webResponse.Cookies; } } } while (loginJson.captcha_needed || loginJson.emailauth_needed); // If the login was successful, we need to enter the cookies to steam. if (loginJson.success) { _cookies = new CookieContainer(); foreach (Cookie cookie in cookieCollection) { _cookies.Add(cookie); } SubmitCookies(_cookies); return(true); } else { Console.WriteLine("SteamWeb Error: " + loginJson.message); return(false); } }
/// <summary> /// 获取数据代码段 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnGet_Click(object sender, EventArgs e) { Dictionary<string, string> Cat = DictCreate(); foreach (KeyValuePair<string, string> cat in Cat) { string keyname = cat.Key.ToString(); string keyvalue = cat.Value.ToString(); HtmlWeb web = new HtmlWeb(); for (int i = 1; i <=10000; i += 20) //{需要考虑年份和类别,结果输出是一年一个表,然后表内是这一年内各个学科的各种期刊数据 { string url = "http://admin-apps.webofknowledge.com/JCR/JCR?RQ=LIST_SUMMARY_JOURNAL&cursor="+i; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); //string postdata = "query_data=" + keyname + "+&RQ=LIST_SUMMARY_JOURNAL&journal_sort_by=title&category_sort_by=cat_title&query_type=category&query_new=true&Submit.x=54&Submit.y=15"; string postdata = "edition=science&science_year=2009&RQ=SELECT&view=category&change_limits=&Submit.x=1&SID=2DLcJKLIPfCO8ImC37p&query_new=true" + "query_data=" + keyname + "+&RQ=LIST_SUMMARY_JOURNAL&journal_sort_by=title&category_sort_by=cat_title&query_type=category&query_new=true&Submit.x=10&Submit.y=9"; byte[] requestBytes = System.Text.Encoding.UTF8.GetBytes(postdata); request.Method = "POST"; CookieContainer cookieContainer = new CookieContainer(); cookieContainer.Add(new Cookie("jcrsid", "2DLcJKLIPfCO8ImC37p", "/", "admin-apps.webofknowledge.com")); request.CookieContainer = cookieContainer; request.Accept = "text/html, application/xhtml+xml, */*"; request.UserAgent = " Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"; request.ContentType = "application/x-www-form-urlencoded"; request.Host = "admin-apps.webofknowledge.com"; Stream requestStream = request.GetRequestStream(); requestStream.Write(requestBytes, 0, requestBytes.Length); requestStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string text = new StreamReader(response.GetResponseStream()).ReadToEnd(); HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(text); HtmlNode rootNode = doc.DocumentNode; //HtmlNodeCollection trs = rootNode.SelectNodes("//table");取出所有的table元素 HtmlNode a = rootNode.SelectSingleNode("//table[@bordercolor='#CCCCCC']");//选出包含索要数据的table HtmlNodeCollection trs = a.SelectNodes(".//tr[position()>2]"); if(trs!=null) { foreach (HtmlNode tr in trs) { JournalDataAll journaldataall = new JournalDataAll(); journaldataall.JournalTitle = CheckValue(tr.SelectSingleNode(".//td[3]").InnerText); journaldataall.Issn = CheckValue(tr.SelectSingleNode(".//td[4]").InnerText); journaldataall.TotalCites = Convert.ToInt32(CheckValue(tr.SelectSingleNode(".//td[5]").InnerText)); journaldataall.ImpactFactor = Convert.ToDouble(CheckValue(tr.SelectSingleNode(".//td[6]").InnerText)); journaldataall.FiveYearIm = Convert.ToDouble(CheckValue(tr.SelectSingleNode(".//td[7]").InnerText)); journaldataall.ImmediacyIndex = Convert.ToDouble(CheckValue(tr.SelectSingleNode(".//td[8]").InnerText)); journaldataall.Articles = Convert.ToInt32(CheckValue(tr.SelectSingleNode(".//td[9]").InnerText)); journaldataall.CitedHalfLife = CheckValue(tr.SelectSingleNode(".//td[10]").InnerText); journaldataall.EigenfactorScore = Convert.ToDouble(CheckValue(tr.SelectSingleNode(".//td[11]").InnerText)); journaldataall.ArticleInfluenceScore = Convert.ToDouble(CheckValue(tr.SelectSingleNode(".//td[12]").InnerText)); journaldataall.Category = keyvalue; journaldatas.Add(journaldataall); } }//对应if(trs!=null) else { break; }//对应else }// for (int i = 1; i <=10000; i += 20) } DataSet dsss = CreateDataset(journaldatas); doExport(dsss.Tables[0], "data","2009"); }
/// <summary> /// Adds a custom cookie to request header. /// </summary> /// <param name="CookieName">Name of the cookie.</param> /// <param name="CookieValue">Value of the cookie.</param> public JoltDeleteRequest SetCookies(string CookieName, string CookieValue) { cookieContainer.Add(new Cookie(CookieName, CookieValue)); return(this); }
void IHttpHandler.ProcessRequest(HttpContext Context) { string ServerURL = ""; string ext = ".tunnel"; string protocol = "http://"; try { // Parsing incoming URL and extracting original server URL char[] URL_Separator = { '/' }; string[] URL_List = Context.Request.Url.AbsoluteUri.Remove(0, protocol.Length).Split(URL_Separator); ServerURL = protocol + URL_List[2].Remove(URL_List[2].Length - ext.Length, ext.Length) + @"/"; string URLPrefix = @"/" + URL_List[1] + @"/" + URL_List[2]; // Eg. "/handler/stg2web.tunnel"; for ( int i = 3; i < URL_List.Length; i++ ) { ServerURL += URL_List[i] + @"/"; } ServerURL = ServerURL.Remove(ServerURL.Length -1, 1); WriteLog(ServerURL + " (" + Context.Request.Url.ToString() + ")"); // Extracting POST data from incoming request Stream RequestStream = Context.Request.InputStream; byte[] PostData = new byte[Context.Request.InputStream.Length]; RequestStream.Read(PostData, 0, (int) Context.Request.InputStream.Length); // Creating proxy web request HttpWebRequest ProxyRequest = (HttpWebRequest) WebRequest.Create(ServerURL); if (false) { ProxyRequest.Proxy = new WebProxy("proxy1:80", true); } ProxyRequest.Method = Context.Request.HttpMethod; ProxyRequest.UserAgent = Context.Request.UserAgent; CookieContainer ProxyCookieContainer = new CookieContainer(); ProxyRequest.CookieContainer = new CookieContainer(); ProxyRequest.CookieContainer.Add(ProxyCookieContainer.GetCookies(new Uri(ServerURL))); ProxyRequest.KeepAlive = true; // For POST, write the post data extracted from the incoming request if (ProxyRequest.Method == "POST") { ProxyRequest.ContentType = "application/x-www-form-urlencoded"; ProxyRequest.ContentLength = PostData.Length; Stream ProxyRequestStream = ProxyRequest.GetRequestStream(); ProxyRequestStream.Write(PostData, 0, PostData.Length); ProxyRequestStream.Close(); } // Getting response from the proxy request HttpWebResponse ProxyResponse = (HttpWebResponse) ProxyRequest.GetResponse(); if (ProxyRequest.HaveResponse) { // Handle cookies foreach(Cookie ReturnCookie in ProxyResponse.Cookies) { bool CookieFound = false; foreach(Cookie OldCookie in ProxyCookieContainer.GetCookies(new Uri(ServerURL))) { if (ReturnCookie.Name.Equals(OldCookie.Name)) { OldCookie.Value = ReturnCookie.Value; CookieFound = true; } } if (!CookieFound) { ProxyCookieContainer.Add(ReturnCookie); } } } Stream StreamResponse = ProxyResponse.GetResponseStream(); int ResponseReadBufferSize = 256; byte[] ResponseReadBuffer = new byte[ResponseReadBufferSize]; MemoryStream MemoryStreamResponse = new MemoryStream(); int ResponseCount = StreamResponse.Read(ResponseReadBuffer, 0, ResponseReadBufferSize); while ( ResponseCount > 0 ) { MemoryStreamResponse.Write(ResponseReadBuffer, 0, ResponseCount); ResponseCount = StreamResponse.Read(ResponseReadBuffer, 0, ResponseReadBufferSize); } byte[] ResponseData = MemoryStreamResponse.ToArray(); string ResponseDataString = Encoding.ASCII.GetString(ResponseData); Context.Response.ContentType = ProxyResponse.ContentType; // While rendering HTML, parse and modify the URLs present if (ProxyResponse.ContentType.StartsWith("text/html")) { HTML.ParseHTML Parser = new HTML.ParseHTML(); Parser.Source = ResponseDataString; while (!Parser.Eof()) { char ch = Parser.Parse(); if (ch == 0) { HTML.AttributeList Tag = Parser.GetTag(); if (Tag.Name.Equals("img", StringComparison.InvariantCultureIgnoreCase)) { // Adjust image tags } if (Tag["href"] != null) { if (Tag["href"].Value.StartsWith(@"/")) { WriteLog("URL " + Tag["href"].Value + " modified to " + URLPrefix + Tag["href"].Value); ResponseDataString = ResponseDataString.Replace( "\"" + Tag["href"].Value + "\"", "\"" + URLPrefix + Tag["href"].Value + "\""); } } if (Tag["src"] != null) { if (Tag["src"].Value.StartsWith(@"/")) { WriteLog("URL " + Tag["src"].Value + " modified to " + URLPrefix + Tag["src"].Value); ResponseDataString = ResponseDataString.Replace( "\"" + Tag["src"].Value + "\"", "\"" + URLPrefix + Tag["src"].Value + "\""); } } } } // Replace script/style file url's ResponseDataString = ResponseDataString.Replace( "url('/", "url('" + URLPrefix + "/"); // replace flash file url's ResponseDataString = ResponseDataString.Replace( "swf: '/", "swf: '" + URLPrefix + "/"); Context.Response.Write(ResponseDataString); } else { Context.Response.OutputStream.Write(ResponseData, 0, ResponseData.Length); } MemoryStreamResponse.Close(); StreamResponse.Close(); ProxyResponse.Close(); } catch (Exception Ex) { Context.Response.Write(Ex.Message.ToString()); WriteLog("An error has occurred while requesting the URL " + ServerURL + "(" + Context.Request.Url.ToString() + ")\n" + Ex.ToString()); } }
private string GetWantedPage() { string loginURL = "https://www.bricklink.com/login.asp"; string wantedURL = "http://www.bricklink.com/wantedDetail.asp?viewFrom=wantedSearch&wantedSize=1000"; string formParams = String.Format("a=a&logFrmFlag=Y&frmUsername={0}&frmPassword={1}", unBox.Text, pwBox.Text); string cookieHeader; string pageSource; try { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(loginURL); req.Timeout = 15000; req.CookieContainer = cookies; req.ContentType = "application/x-www-form-urlencoded"; req.Method = "POST"; req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0"; byte[] bytes = Encoding.ASCII.GetBytes(formParams); req.ContentLength = bytes.Length; int pagefail = 0; bool pagesuccess = false; while ((pagesuccess == false) && (pagefail < 4)) { try { using (Stream os = req.GetRequestStream()) { os.Write(bytes, 0, bytes.Length); pagesuccess = true; } } catch { ImportStatus("Retrying...", Color.Red); pagefail++; } } if (pagesuccess == false) { return("##PageFail##"); } HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); cookieHeader = resp.Headers["Set-cookie"]; cookies.Add(resp.Cookies); using (StreamReader sr = new StreamReader(resp.GetResponseStream())) { pageSource = sr.ReadToEnd(); } Match loginError = Regex.Match(pageSource, @"Invalid Password.", RegexOptions.IgnoreCase); if (loginError.Success) { return(null); } HttpWebRequest wantedReq = (HttpWebRequest)WebRequest.Create(wantedURL); wantedReq.Timeout = 15000; wantedReq.CookieContainer = cookies; wantedReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0"; pagefail = 0; pagesuccess = false; while ((pagesuccess == false) && (pagefail < 4)) { try { HttpWebResponse getResponse = (HttpWebResponse)wantedReq.GetResponse(); using (StreamReader sr = new StreamReader(getResponse.GetResponseStream())) { pageSource = sr.ReadToEnd(); } pagesuccess = true; } catch { ImportStatus("Retrying...", Color.Red); pagefail++; } } if (pagesuccess == false) { return("##PageFail##"); } } catch (Exception e) { ImportStatus("Failed to retrieve wanted list.", Color.Red); MessageBox.Show("Failed to retrieve wanted list:" + Environment.NewLine + e.Message); return("##PageFail##"); } return(pageSource); }
public async Task GetAsync_SetCookieContainer_CookieSent(string cookieName, string cookieValue) { var handler = new HttpClientHandler(); var cookieContainer = new CookieContainer(); cookieContainer.Add(Configuration.Http.RemoteEchoServer, new Cookie(cookieName, cookieValue)); handler.CookieContainer = cookieContainer; using (var client = new HttpClient(handler)) { using (HttpResponseMessage httpResponse = await client.GetAsync(Configuration.Http.RemoteEchoServer)) { Assert.Equal(HttpStatusCode.OK, httpResponse.StatusCode); string responseText = await httpResponse.Content.ReadAsStringAsync(); _output.WriteLine(responseText); Assert.True(TestHelper.JsonMessageContainsKeyValue(responseText, cookieName, cookieValue)); } } }
/// <summary> /// Adds a custom cookie to request header. /// </summary> /// <param name="CookieName">Name of the cookie.</param> /// <param name="CookieValue">Value of the cookie.</param> public JoltPostText SetCookies(string CookieName, string CookieValue) { cookieContainer.Add(new Cookie(CookieName, CookieValue)); return(this); }
public static void Capacity_ShrinkNoneExpired_RemovesAll() { Cookie c1 = new Cookie("name1", "value", "", ".url1.com"); Cookie c2 = new Cookie("name2", "value", "", ".url2.com"); Cookie c3 = new Cookie("name3", "value", "", ".url3.com"); Cookie c4 = new Cookie("name4", "value", "", ".url4.com"); CookieContainer cc = new CookieContainer() { PerDomainCapacity = 1 }; cc.Add(c1); cc.Add(c2); cc.Add(c3); cc.Add(c4); // Shrink to a number less than the current count - since we have no cookies that have expired, // we should just clear the whole thing except for a single cookie cc.Capacity = 2; Assert.Equal(2, cc.Capacity); Assert.Equal(0, cc.Count); }
/// <summary> /// Get数据 /// </summary> public string GetData(string URL) { HTML = string.Empty; contentType = string.Empty; statusCode = 0; //防止出错 System.Net.ServicePointManager.Expect100Continue = false; try { webRequest = HttpWebRequest.Create(URL) as HttpWebRequest; webRequest.Accept = this.accept; webRequest.UserAgent = this.userAgent; webRequest.Method = "GET"; webRequest.Timeout = this.timeOut; CookieContainer mc = new CookieContainer(); for (int i = 0; i < cc.Count; i++) { cc[i].Domain = webRequest.RequestUri.Host; } mc.Add(cc); webRequest.CookieContainer = mc; webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.Headers.Add("Accept-Encoding", "gzip,deflate"); webRequest.Headers.Add("Accept-Language", "zh-cn"); webRequest.Headers.Add("Cache-Control", "no-cache"); webRequest.Headers.Add("UA-CPU", "x86"); if (isXMLHttpRequest) { webRequest.Headers.Add("x-requested-with", "XMLHttpRequest"); } webRequest.KeepAlive = false; webResponse = (HttpWebResponse)webRequest.GetResponse(); cc.Add(webResponse.Cookies); cc.Add(mc.GetCookies(webRequest.RequestUri)); if (webResponse.ContentLength == 0) { return(HTML); } Encoding encoding = Encoding.UTF8; if (this.character != string.Empty) { encoding = Encoding.GetEncoding(character); } else { if (webResponse.CharacterSet.Trim() != "") { encoding = Encoding.GetEncoding(webResponse.CharacterSet); } } contentType = webResponse.Headers["Content-Type"]; Stream receiveStream = null; //取得返回的编码方式 string reContentEncoding = webResponse.ContentEncoding; if (reContentEncoding == "gzip") { receiveStream = new GZipStream(webResponse.GetResponseStream(), CompressionMode.Decompress); } else if (reContentEncoding == "deflate") { receiveStream = new DeflateStream(webResponse.GetResponseStream(), CompressionMode.Decompress); } else { receiveStream = webResponse.GetResponseStream(); } StreamReader streamReader = new StreamReader(receiveStream, encoding); if (contentType.StartsWith("text", StringComparison.CurrentCultureIgnoreCase)) { HTML = streamReader.ReadToEnd(); } //转化为UTF-8 Encoding utf8 = Encoding.Unicode; byte[] responseEncodingBytes = encoding.GetBytes(HTML); byte[] utf8Bytes = Encoding.Convert(encoding, utf8, responseEncodingBytes); HTML = utf8.GetString(utf8Bytes); streamReader.Close(); webResponse.Close(); isSucc = true; } catch (Exception ex) { isSucc = false; errorString = ex.Message; } finally { } return(HTML); }
public static void Add_ReachedMaxDomainCapacity_NotAdded() { CookieContainer cc = new CookieContainer() { PerDomainCapacity = 1 }; cc.Add(c1); // Add a cookie to fill up the capacity Assert.Equal(1, cc.Count); cc.Add(c2); // Should not be added Assert.Equal(1, cc.Count); }
/// <summary> /// Post数据 /// </summary> public string PostData(string URL, string Data) { HTML = string.Empty; contentType = string.Empty; statusCode = 0; System.Net.ServicePointManager.Expect100Continue = false; try { webRequest = HttpWebRequest.Create(URL) as HttpWebRequest; webRequest.Accept = this.accept; webRequest.UserAgent = this.userAgent; webRequest.Method = "POST"; webRequest.Timeout = this.timeOut; if (character == string.Empty) { character = "UTF-8"; } CookieContainer mc = new CookieContainer(); for (int i = 0; i < cc.Count; i++) { cc[i].Domain = webRequest.RequestUri.Host; } mc.Add(cc); webRequest.CookieContainer = mc; webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.Headers.Add("Accept-Encoding", "gzip, deflate"); webRequest.Headers.Add("Accept-Language", "zh-cn"); webRequest.Headers.Add("Cache-Control", "no-cache"); webRequest.Headers.Add("UA-CPU", "x86"); if (isXMLHttpRequest) { webRequest.Headers.Add("x-requested-with", "XMLHttpRequest"); } webRequest.KeepAlive = false; webRequest.AllowAutoRedirect = true; webRequest.MaximumAutomaticRedirections = 5; Encoding encoding = Encoding.GetEncoding(character); byte[] byteData = encoding.GetBytes(Data); webRequest.ContentLength = byteData.Length; //发送数据 Stream stream = webRequest.GetRequestStream(); stream.Write(byteData, 0, byteData.Length);; stream.Close(); //收接数据 webResponse = (HttpWebResponse)webRequest.GetResponse(); statusCode = (int)webResponse.StatusCode; contentLength = webResponse.ContentLength; contentType = webResponse.ContentType; cc.Add(webResponse.Cookies); cc.Add(mc.GetCookies(webRequest.RequestUri)); Stream receiveStream = null; string reContentEncoding = webResponse.ContentEncoding;//取得返回的编码方式 if (reContentEncoding == "gzip") { receiveStream = new GZipStream(webResponse.GetResponseStream(), CompressionMode.Decompress); } else if (reContentEncoding == "deflate") { receiveStream = new DeflateStream(webResponse.GetResponseStream(), CompressionMode.Decompress); } else { receiveStream = webResponse.GetResponseStream(); } StreamReader streamReader = new StreamReader(receiveStream, encoding); HTML = streamReader.ReadToEnd(); //转化为UTF-8 Encoding utf8 = Encoding.Unicode; byte[] responseEncodingBytes = encoding.GetBytes(HTML); byte[] utf8Bytes = Encoding.Convert(encoding, utf8, responseEncodingBytes); HTML = utf8.GetString(utf8Bytes); receiveStream.Close(); streamReader.Close(); webResponse.Close(); isSucc = true; } catch (Exception ex) { isSucc = false; errorString = ex.Message; } finally { } return(HTML); }
public static async void Add_ReachedMaxCountWithExpiredCookies_Added() { Cookie c1 = new Cookie("name1", "value", "", ".domain1.com"); Cookie c2 = new Cookie("name2", "value", "", ".domain2.com"); Cookie c3 = new Cookie("name3", "value", "", ".domain3.com"); c1.Expires = DateTime.Now.AddSeconds(1); // The cookie will expire in 1 second CookieContainer cc = new CookieContainer(2); cc.Add(c1); cc.Add(c2); await Task.Delay(2000); // Sleep for 2 seconds to wait for the cookie to expire cc.Add(c3); Assert.Equal(0, cc.GetCookies(new Uri("http://domain1.com")).Count); Assert.Equal(c3, cc.GetCookies(new Uri("http://domain3.com"))[0]); }
public static byte[] SendPostRequestAndSetCookies(string url, string body) { try { //Log.WriteLogByDate("调用URL:" + url); //Log.WriteLogByDate(body); byte[] request_body = Encoding.UTF8.GetBytes(body); System.Net.ServicePointManager.DefaultConnectionLimit = 512; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); //request.Headers.Add(HttpRequestHeader.UserAgent.ToString(), "Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5"); request.Method = "post"; request.ContentLength = request_body.Length; Stream request_stream = request.GetRequestStream(); request_stream.Write(request_body, 0, request_body.Length); if (CookiesContainer == null) { CookiesContainer = new CookieContainer(); } request.CookieContainer = CookiesContainer; //启用cookie HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream response_stream = response.GetResponseStream(); if (CookiesContainer == null) { CookiesContainer = new CookieContainer(); CookiesContainer.Add(response.Cookies); } //foreach (Cookie cookie in response.Cookies) //{ // CookiesContainer.SetCookies(cookie.CommentUri); //} if (response.StatusCode.ToString() == "OK") { string setCookie = response.Headers.Get("Set-Cookie"); Log.WriteLogByDate(setCookie); if (setCookie != null) { if (setCookie.Contains("wx.qq.com")) { //CookiesContainer.SetCookies(new Uri("https://wx.qq.com"), setCookie); CookiesContainer.Add(GetAllCookiesFromHeader(setCookie, "https://wx.qq.com")); } if (setCookie.Contains("wx2.qq.com")) { // CookiesContainer.SetCookies(new Uri("https://wx2.qq.com"), setCookie.ToString()); //CookiesContainer.SetCookies(new Uri("https://wx2.qq.com"), "webwx_data_ticket=gSdewl4ryJSJOY8TaCLXgXQ/; Domain=.qq.com; Path=/; Expires=Sat, 03-Dec-2016 02:47:24 GMT"); CookiesContainer.Add(GetAllCookiesFromHeader(setCookie, "https://wx2.qq.com")); } } } int count = (int)response.ContentLength; int offset = 0; byte[] buf = new byte[count]; while (count > 0) //读取返回数据 { int n = response_stream.Read(buf, offset, count); if (n == 0) { break; } count -= n; offset += n; } List <Cookie> list = GetAllCookies(CookiesContainer); //response.Close(); return(buf); } catch (Exception ex) { Log.WriteLog(ex); return(null); } }
public static void Add_Cookie_Invalid() { CookieContainer cc = new CookieContainer(); Assert.Throws<ArgumentNullException>(() => cc.Add((Cookie)null)); // Null cookie Assert.Throws<ArgumentException>(() => cc.Add(new Cookie("name", "value", "", ""))); // Empty domain cc.MaxCookieSize = 1; Assert.Throws<CookieException>(() => cc.Add(new Cookie("name", "long-text", "", "contoso.com"))); // Value.Length > MaxCookieSize }
public static byte[] SendGetRequestAndSetCookies(string url) { try { System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); System.Net.ServicePointManager.DefaultConnectionLimit = 512; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "get"; //if (CookiesContainer != null) //{ // request.CookieContainer = CookiesContainer; //} //else //{ // request.CookieContainer = new CookieContainer(); //} //request.CookieContainer = CookiesContainer; //启用cookie if (CookiesContainer == null) { CookiesContainer = new CookieContainer(); } request.CookieContainer = CookiesContainer; //启用cookie HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (CookiesContainer == null) { CookiesContainer = new CookieContainer(); CookiesContainer.Add(response.Cookies);; } Stream response_stream = response.GetResponseStream(); ////输出cookieContainer //Log.WriteLogByDate("GetUrl:" + url); //List<Cookie> list = GetAllCookies(CookiesContainer); //foreach (Cookie item in list) //{ // Log.WriteLogByDate("Name:" + item.Name + " Value: " + item.Value); //} ////输出header //WebHeaderCollection headers = request.Headers; //foreach (var key in headers.AllKeys) //{ // Log.WriteLogByDate("Header: key=" + key + " value:" + headers.GetValues(key)[0]); //} int count = (int)response.ContentLength; int offset = 0; byte[] buf = new byte[count]; while (count > 0) //读取返回数据 { int n = response_stream.Read(buf, offset, count); if (n == 0) { break; } count -= n; offset += n; } return(buf); } catch (Exception ex) { Log.WriteLog(ex); return(null); } }
public static void Add_VerificationFailedCookie_Throws(Cookie c) { CookieContainer cc = new CookieContainer(); Assert.Throws<CookieException>(() => cc.Add(c)); }
static bool checkConnection() { string url = "http://rarbg.to/index5.php"; bool success = false; string str; HttpWebRequest request = null; HttpWebResponse response = null; Console.WriteLine("CHECKCONNECT:" + "http://rarbg.to/index5.php"); StreamReader streamReader = null; try { CookieContainer cookieContainer = new CookieContainer(); Cookie lastVisit = new Cookie("LastVisit", Config1.getLastVisit(), "/", "rarbg.to"); Cookie __utma = new Cookie("__utma", "211336342.1333136546.1369105449.1369109171.1369112684.3", "/", "rarbg.to"); Cookie __utmb = new Cookie("__utmb", "211336342.5.10.1369112684", "/", "rarbg.to"); Cookie __utmc = new Cookie("__utmc", "211336342", "/", "rarbg.to"); Cookie __utmz = new Cookie("__utmz", "211336342.1369105449.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)", "/", "rarbg.to"); Cookie bSbTZF2j = new Cookie("bSbTZF2j", "6BdPQ9qs", "/", "rarbg.to"); cookieContainer.Add(lastVisit); cookieContainer.Add(bSbTZF2j); cookieContainer.Add(__utma); cookieContainer.Add(__utmb); cookieContainer.Add(__utmc); cookieContainer.Add(__utmz); request = (HttpWebRequest)WebRequest.Create(url); request.CookieContainer = cookieContainer; request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36"; request.Timeout = 15000; request.KeepAlive = false; request.Referer = "http://rarbg.to/torrent/j1kx3ny"; response = (HttpWebResponse)request.GetResponse(); if (response.Cookies["LastVisit"] != null) Config1.setLastVisit(response.Cookies["LastVisit"].ToString()); Stream streamReceive = response.GetResponseStream(); Encoding encoding = Encoding.GetEncoding("GB2312"); streamReader = new StreamReader(streamReceive, encoding); str = streamReader.ReadToEnd(); success = true; Console.WriteLine("检测成功"); } catch (Exception ex) { Console.WriteLine(ex.Message + ":检测失败:" + url); } finally { if (request != null) request.Abort(); if (streamReader != null) streamReader.Close(); if (response != null) response.Close(); Thread.Sleep(1000); } return success; }
public static void Add_CookieCollectionUri_Invalid() { CookieContainer cc = new CookieContainer(); Assert.Throws<ArgumentNullException>(() => cc.Add(null, new CookieCollection())); //Null uri Assert.Throws<ArgumentNullException>(() => cc.Add(new Uri("http://contoso.com"), (CookieCollection)null)); //Null collection }
private HttpWebRequest GenerateRequest(ConnectionOptions co) { var request = (HttpWebRequest)WebRequest.Create(co.source); // set user agent if (!string.IsNullOrEmpty(co.userAgent)) { request.UserAgent = co.userAgent; } // set proxy if (co.proxy != null) { request.Proxy = co.proxy; } if (co.useHttp10) { request.ProtocolVersion = HttpVersion.Version10; } // set timeout value for the request request.Timeout = request.ServicePoint.ConnectionLeaseTimeout = request.ServicePoint.MaxIdleTime = co.requestTimeout; request.AllowAutoRedirect = true; request.AllowWriteStreamBuffering = true; request.AllowAutoRedirect = true; request.KeepAlive = true; request.SendChunked = false; // set login and password if (!string.IsNullOrEmpty(co.username)) { request.Credentials = new NetworkCredential(co.username, co.password); } // set connection group name if (co.useSeparateConnectionGroup) { request.ConnectionGroupName = Guid.NewGuid().ToString(); } // force basic authentication through extra headers if required var authInfo = ""; if (!string.IsNullOrEmpty(co.username)) { authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(co.username + ":" + co.password)); request.Headers["Authorization"] = "Basic " + authInfo; } if (!string.IsNullOrEmpty(co.cookies)) { co.cookies = co.cookies.Replace("[AUTH]", authInfo); co.cookies = co.cookies.Replace("[USERNAME]", co.username); co.cookies = co.cookies.Replace("[PASSWORD]", co.password); co.cookies = co.cookies.Replace("[CHANNEL]", co.channel); var myContainer = new CookieContainer(); string[] coll = co.cookies.Split(';'); foreach (var ckie in coll) { if (!string.IsNullOrEmpty(ckie)) { string[] nv = ckie.Split('='); if (nv.Length == 2) { var cookie = new Cookie(nv[0].Trim(), nv[1].Trim()); myContainer.Add(new Uri(request.RequestUri.ToString()), cookie); } } } request.CookieContainer = myContainer; } if (!string.IsNullOrEmpty(co.headers)) { co.headers = co.headers.Replace("[AUTH]", authInfo); string[] coll = co.headers.Split(';'); foreach (var hdr in coll) { if (!string.IsNullOrEmpty(hdr)) { string[] nv = hdr.Split('='); if (nv.Length == 2) { request.Headers.Add(nv[0], nv[1]); } } } } return(request); }
public static async void GetCookies_RemovesExpired_Cookies() { Cookie c1 = new Cookie("name1", "value", "", ".url1.com"); Cookie c2 = new Cookie("name2", "value", "", ".url2.com"); c1.Expires = DateTime.Now.AddSeconds(1); // The cookie will expire in 1 second CookieContainer cc = new CookieContainer(); cc.Add(c1); cc.Add(c2); await Task.Delay(2000); // Sleep for 2 seconds to wait for the cookie to expire Assert.Equal(0, cc.GetCookies(new Uri("http://url1.com")).Count); // There should no longer be such a cookie }
protected void btnGet_Click(object sender, EventArgs e) { // //string postdata = "edition=science&science_year=2011&view=category&RQ=SELECT_ALL&change_limits=&Submit.x=1&SID=V2gNFOjAf5oiD1g75HK&query_new=true"; // string url = "http://admin-apps.webofknowledge.com/JCR/JCR"; // HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); // string s = "edition=science&science_year=2011&view=category&RQ=SELECT_ALL&change_limits=&Submit.x=1&SID=P1imB6kl5KLe8L1h2iI&query_new=true"; // //CookieContainer cookieContainer = new CookieContainer(); // byte[] requestBytes = System.Text.Encoding.UTF8.GetBytes(s); // req.Method = "POST"; // req.Accept = "text/html, application/xhtml+xml, */*"; // req.UserAgent = " Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"; // req.ContentType = "application/x-www-form-urlencoded"; // req.ContentLength = requestBytes.Length; // req.Referer = "http://admin-apps.webofknowledge.com/JCR/JCR?SID=P1imB6kl5KLe8L1h2iI"; //// req.CookieContainer = cookieContainer; // Stream requestStream = req.GetRequestStream(); // requestStream.Write(requestBytes, 0, requestBytes.Length); // requestStream.Close(); // HttpWebResponse res = (HttpWebResponse)req.GetResponse(); // StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.UTF8); // string backstr = sr.ReadToEnd(); HtmlWeb web = new HtmlWeb(); // List<JournalData> journaldatas = new List<JournalData>(); for (int i = 6001; i <= 8321; i += 20)//8321 //for (int i = 1; i <= 20; i += 20) { string url = "http://admin-apps.webofknowledge.com/JCR/JCR?RQ=SELECT_ALL&cursor="+i; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); CookieContainer cookieContainer = new CookieContainer(); cookieContainer.Add(new Cookie("jcrsid", "P2LF2BOHJ@G8H4khk2n", "/", "admin-apps.webofknowledge.com")); request.CookieContainer = cookieContainer; request.Accept = "text/html, application/xhtml+xml, */*"; request.UserAgent = " Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"; request.ContentType = "application/x-www-form-urlencoded"; request.Host = "admin-apps.webofknowledge.com"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string text = new StreamReader(response.GetResponseStream()).ReadToEnd(); HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(text); HtmlNode rootNode = doc.DocumentNode; //HtmlNodeCollection trs = rootNode.SelectNodes("//table");取出所有的table元素 HtmlNode a = rootNode.SelectSingleNode("//table[@bordercolor='#CCCCCC']");//选出包含索要数据的table HtmlNodeCollection trs = a.SelectNodes(".//tr[position()>2]"); foreach(HtmlNode tr in trs) { JournalData journaldata = new JournalData(); journaldata.JournalTitle = CheckValue(tr.SelectSingleNode(".//td[3]").InnerText); journaldata.Issn = CheckValue(tr.SelectSingleNode(".//td[4]").InnerText); journaldata.TotalCites = Convert.ToInt32(CheckValue(tr.SelectSingleNode(".//td[5]").InnerText)); journaldata.ImpactFactor = Convert.ToDouble(CheckValue(tr.SelectSingleNode(".//td[6]").InnerText)); journaldata.FiveYearIm = Convert.ToDouble(CheckValue(tr.SelectSingleNode(".//td[7]").InnerText)); journaldata.ImmediacyIndex = Convert.ToDouble(CheckValue(tr.SelectSingleNode(".//td[8]").InnerText)); journaldata.Articles = Convert.ToInt32(CheckValue(tr.SelectSingleNode(".//td[9]").InnerText)); journaldata.CitedHalfLife = CheckValue(tr.SelectSingleNode(".//td[10]").InnerText); journaldata.EigenfactorScore = Convert.ToDouble(CheckValue(tr.SelectSingleNode(".//td[11]").InnerText)); journaldata.ArticleInfluenceScore = Convert.ToDouble(CheckValue(tr.SelectSingleNode(".//td[12]").InnerText)); journaldatas.Add(journaldata); } } // rtp.DataSource = journaldatas; // rtp.DataBind(); //SendToTxt(journaldatas,"test"); DataSet dsss = CreateDataset(journaldatas); doExport(dsss.Tables[0],"data"); }