public void GetReady () { col = new CookieCollection (); col.Add (new Cookie ("name1", "value1")); col.Add (new Cookie ("name2", "value2", "path2")); col.Add (new Cookie ("name3", "value3", "path3", "domain3")); }
public void Add () { try { Cookie c = null; col.Add (c); Assert.Fail ("#1"); } catch (ArgumentNullException) { } // in the microsoft implementation this will fail, // so we'll have to fail to. try { col.Add (col); Assert.Fail ("#2"); } catch (Exception) { } Assert.AreEqual (col.Count, 3, "#3"); col.Add (new Cookie("name1", "value1")); Assert.AreEqual (col.Count, 3, "#4"); CookieCollection col2 = new CookieCollection(); Cookie c4 = new Cookie("name4", "value4"); Cookie c5 = new Cookie("name5", "value5"); col2.Add (c4); col2.Add (c5); col.Add (col2); Assert.AreEqual (col.Count, 5, "#5"); Assert.AreEqual (col ["NAME4"], c4, "#6"); Assert.AreEqual (col [4], c5, "#7"); }
private 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; }
private CookieCollection LogInAndEstablishSession(TDAmeritradeLoginCredentials creds) { var loginResponse = LoginCallResp(creds); var cc = new CookieCollection(); cc.Add(loginResponse.Cookies); cc.Add(SecurityQuestionCallResp(loginResponse).Cookies); return cc; }
public /*CookieCollection*/ CookieContainer NGetCookie(string myUrl, string UserAgent) { CookieContainer MyCookie = new CookieContainer(); CookieCollection Col = new CookieCollection(); Uri url = new Uri(myUrl); HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); //if (_useProxy) //{ // httpWebRequest.Proxy = new WebProxy(_ipProxy, _portProxy); // if (_ipProxy != "127.0.0.1" && _portProxy != 8118) // httpWebRequest.Proxy.Credentials = new System.Net.NetworkCredential("UA_NALfVz2iFnd9", "AXz1M8FV2E"); //} httpWebRequest.UserAgent = UserAgent; //"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36"; httpWebRequest.CookieContainer = new CookieContainer(); if (Col != null) { httpWebRequest.CookieContainer.Add(Col); } HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); httpWebResponse.Cookies = httpWebRequest.CookieContainer.GetCookies(httpWebRequest.RequestUri); if (httpWebResponse.Cookies != null) { Col.Add(httpWebResponse.Cookies); } MyCookie.Add(Col); //return Col; return MyCookie; }
protected override CookieImportResult ProtectedGetCookies(Uri targetUrl) { if (IsAvailable == false) return new CookieImportResult(null, CookieImportState.Unavailable); try { var cookies = new CookieCollection(); var res = CookieImportState.ConvertError; using (var sr = new System.IO.StreamReader(SourceInfo.CookiePath)) while (!sr.EndOfStream) { var line = sr.ReadLine(); if (line.StartsWith("cookies=")) cookies.Add(ParseCookieSettings(line)); res = CookieImportState.Success; } return new CookieImportResult(cookies, res); } catch (System.IO.IOException ex) { TraceError(this, "読み込みでエラーが発生しました。", ex.ToString()); return new CookieImportResult(null,CookieImportState.AccessError); } catch (Exception ex) { TraceError(this, "読み込みでエラーが発生しました。", ex.ToString()); return new CookieImportResult(null, CookieImportState.ConvertError); } }
/// <summary> /// urlに関連付けられたクッキーを取得します。 /// </summary> public override CookieCollection GetCookieCollection(Uri url) { // 関係のあるファイルだけ調べることによってパフォーマンスを向上させる List<string> files = SelectFiles(url, GetAllFiles()); List<Cookie> cookies = new List<Cookie>(); foreach (string filepath in files) { cookies.AddRange(PickCookiesFromFile(filepath)); } // Expiresが最新のもので上書きする cookies.Sort(CompareCookieExpiresAsc); CookieCollection collection = new CookieCollection(); foreach (Cookie cookie in cookies) { try { collection.Add(cookie); } catch (Exception ex) { CookieGetter.Exceptions.Enqueue(ex); System.Diagnostics.Debug.WriteLine(ex.Message); } } return collection; }
public static string SendHttpData( string pageUrl, string postData, CookieContainer cookieContainer, CookieCollection cookieCollection) { //Console.WriteLine("SendData '{1}' to '{0}'", pageUrl, postData); HttpWebRequest httpWebRequest = (HttpWebRequest) WebRequest.Create(pageUrl); httpWebRequest.Method = "GET"; httpWebRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.17) Gecko/20080829 Firefox/2.0.0.17 (.NET CLR 3.5.30729)"; httpWebRequest.CookieContainer = cookieContainer; httpWebRequest.KeepAlive = true; if (postData != null) { UTF8Encoding encoding = new UTF8Encoding(); byte[] data = encoding.GetBytes(postData); httpWebRequest.Method = "POST"; httpWebRequest.ContentType = "application/x-www-form-urlencoded"; httpWebRequest.ContentLength = data.Length; Stream postStream = httpWebRequest.GetRequestStream(); postStream.Write(data, 0, data.Length); postStream.Close(); } HttpWebResponse httpWebResponse = (HttpWebResponse) httpWebRequest.GetResponse(); cookieCollection.Add(httpWebResponse.Cookies); cookieContainer.Add(cookieCollection); StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8); return streamReader.ReadToEnd(); }
protected override CookieImportResult ProtectedGetCookies(Uri targetUrl) { if (IsAvailable == false) return new CookieImportResult(null,CookieImportState.Unavailable); try { var formatVersionRec = LookupEntry(SourceInfo.CookiePath, SELECT_QUERY_VERSION); int formatVersion; if (formatVersionRec.Count == 0 || formatVersionRec[0].Length == 0 || int.TryParse((string)formatVersionRec[0][0], out formatVersion) == false) return new CookieImportResult(null,CookieImportState.ConvertError); string query; query = formatVersion < 7 ? SELECT_QUERY : SELECT_QUERY_V7; query = string.Format("{0} {1} ORDER BY creation_utc DESC", query, MakeWhere(targetUrl)); var cookies = new CookieCollection(); foreach (var item in LookupCookies(SourceInfo.CookiePath, query, rec => DataToCookie(rec, formatVersion))) cookies.Add(item); return new CookieImportResult(cookies, CookieImportState.Success); } catch (CookieImportException ex) { TraceError(this, "取得に失敗しました。", ex.ToString()); return new CookieImportResult(null, ex.Result); } }
Regex ifParse = new Regex(@"^([^<>=!]+)\s*([!<>=]+)\s*(.+)$", RegexOptions.Compiled); //variable names and operator match for if statement string getProcessorText() { if (string.IsNullOrEmpty(processorUrl) || itemUrl == null) return null; string url = string.Format("{0}?url={1}", processorUrl, HttpUtility.UrlEncode(itemUrl)); CookieCollection ccollection = new CookieCollection(); ccollection.Add(new Cookie("version", version.ToString())); ccollection.Add(new Cookie("platform", platform)); if (processorUrl.StartsWith("http://www.navixtreme.com") && !string.IsNullOrEmpty(nxId)) ccollection.Add(new Cookie("nxid", nxId)); CookieContainer cc = new CookieContainer(); cc.Add(new Uri(url), ccollection); return WebCache.Instance.GetWebData(url, cookies: cc); }
public BaseApiResponse GetCartData(string username, string password) { var l = Login(username, password); var cc = new CookieCollection(); cc.Add(new Cookie("CONSUMERCHOICE", "us/en_us", "/", "nike.com")); cc.Add(new Cookie("NIKE_COMMERCE_COUNTRY", "US", "/", "nike.com")); cc.Add(new Cookie("NIKE_COMMERCE_LANG_LOCALE", "en_US", "/", "nike.com")); cc.Add(new Cookie("nike_locale", "us/en_us", "/", "nike.com")); var hc = CookieHelper.GetAllCookiesFromHeader(l.Headers["Set-Cookie"], "nike.com"); cc.Add(hc); return DoGet(CartUrl, null, cc); }
public void SetCookies(IEnumerable<Cookie> cookies) { _currentCollection = new CookieCollection(); foreach (var cookie in cookies) { _currentCollection.Add(cookie); } CookieContainer.Add(_currentCollection); }
//подсунем кукии сайту citystar тюмень public CookieContainer GetCookieTumenRealtyCityStar(Uri myUrl) { //string[] cook = webBrowser.Document.Cookie.ToString().Split(new Char[] { '=', ';', ' ' }); // получаю массив в виде: UID, 12341424151,,PID,1423523236,... CookieContainer MyCookie = new CookieContainer(); CookieCollection Col = new CookieCollection(); Uri url = myUrl;//new Uri(myUrl); Cookie CS_SESSION_ID = new Cookie("CS_SESSION_ID", "xnyh296mrt4i5to84z9boen5tk26nsz734hku8l19n3yhmm9qy"); Cookie ASPNET_SessionId = new Cookie("ASP.NET_SessionId", "5btohi23q5j4f5h1z1nlof1z"); Col.Add(CS_SESSION_ID); Col.Add(ASPNET_SessionId); //Cookie: CS_SESSION_ID=xnyh296mrt4i5to84z9boen5tk26nsz734hku8l19n3yhmm9qy; ASP.NET_SessionId=5btohi23q5j4f5h1z1nlof1z MyCookie.Add(url, Col); return MyCookie; }
protected override string OnScrape(string url, HtmlNode elem) { var hash = SelectItem(elem, "[name=hash]").Attributes["value"].Value; var id = SelectItem(elem, "[name=id]").Attributes["value"].Value; var fname = SelectItem(elem, "[name=fname]").Attributes["value"].Value; var inhu = SelectItem(elem, "[name=inhu]").Attributes["value"].Value; var data = new NameValueCollection(); data.Add("_vhash", SubstringBetween(elem.InnerHtml, "name: '_vhash', value: '", "'")); data.Add("fname", fname); data.Add("gfk", SubstringBetween(elem.InnerHtml, "name: 'gfk', value: '", "'")); data.Add("hash", hash); data.Add("id", id); data.Add("imhuman", "Proceed to video"); data.Add("inhu", inhu); data.Add("op", "download1"); data.Add("referer", ""); data.Add("usr_login", ""); var cookies = new CookieCollection(); cookies.Add(new Cookie("file_id", SubstringBetween(elem.InnerHtml, "'file_id', '", "'")) { Domain = "www.thevideo.me" }); cookies.Add(new Cookie("aff", SubstringBetween(elem.InnerHtml, "'aff', '", "'")) { Domain = "www.thevideo.me" }); cookies.Add(new Cookie("lang", "1") { Domain = "www.thevideo.me" }); //cookies.Add(new Cookie("ref_url", url) { Domain = "www.thevideo.me" }); //cookies.Add(new Cookie("mlUserID", "dtwVzu8bAWsc") { Domain = "www.thevideo.me" }); //cookies.Add(new Cookie("__cfduid", "d629171dd342b852f8ddabc37c85b978e1406816048462") { Domain = ".thevideo.me" }); var nv = new NameValueCollection(); nv.Add("Referer", url); var c = 0; var eval = String.Empty; var text = Properties.Resources.Unpacker; while (c++ < 6) { elem = Post(url, data, cookies, nv); eval = SubstringBetween(elem.InnerHtml, "eval", "</"); if (!String.IsNullOrEmpty(eval)) break; Thread.Sleep(1000); } eval = "eval" + eval; eval = eval.Replace("\"", "\\x22"); url = SubstringBetween(UnpackScript(text.Replace("X", eval)), "file:'", "'"); return new Uri(url).AbsoluteUri; }
private CookieCollection RepackCookie(ReadOnlyCollection<OpenQA.Selenium.Cookie> cookies) { var pack = new CookieCollection(); foreach (var c in cookies) { pack.Add(new System.Net.Cookie(c.Name, c.Value, c.Path, c.Domain)); } return pack; }
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { if (schalter2 == 1) { string url = "https://battlelog.battlefield.com/bf3/de/gate/login/"; string Parameters = String.Format("redirect={0}&email={1}&password={2}&submit={3}", "|bf3|de|", username, password, "Sign In"); Parameters = System.Web.HttpUtility.UrlEncode(Parameters); CookieContainer _cookieContainer = new CookieContainer(); //erster Login auf der Website HttpWebRequest _request1 = (HttpWebRequest)WebRequest.Create(url); _request1.Method = "POST"; _request1.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; _request1.AllowAutoRedirect = true; _request1.CookieContainer = _cookieContainer; _request1.ContentType = "application/x-www-form-urlencoded"; _request1.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)"; _request1.Headers.Add(HttpRequestHeader.CacheControl, "Set-Cookie" ); string _postData = Parameters; byte[] _byteArray = Encoding.UTF8.GetBytes(_postData); _request1.ContentLength = _byteArray.Length; Stream _reqStream = _request1.GetRequestStream(); _reqStream.Write(_byteArray, 0, _byteArray.Length); _reqStream.Close(); HttpWebResponse _response1 = (HttpWebResponse)_request1.GetResponse(); StreamReader _reader1 = new StreamReader(_response1.GetResponseStream()); CookieCollection _cookieCollection = new CookieCollection(); foreach (Cookie _cook in _response1.Cookies) { _cookieCollection.Add(_cook); } MessageBox.Show(_reader1.ReadToEnd()); //webBrowser1.DocumentText = _response1.ToString(); _response1.Close(); _reader1.Close(); schalter2 = 0; } }
/// <summary> /// Converts to CLR cookies. /// </summary> /// <returns>The to CLR cookies.</returns> /// <param name="platformCookies">Platform cookies.</param> public static CookieCollection ConvertToCLRCookies (object [] platformCookies) { CookieCollection clrCookies = new CookieCollection (); #if PLATFORM_IOS foreach (PlatformCookie cookie in platformCookies) { clrCookies.Add (cookie.ConvertToCLRCookie ()); } #elif PLATFORM_ANDROID throw new NotImplementedException (); #endif return (clrCookies.Count > 0) ? clrCookies : null; }
public static CookieCollection GetRequestCookies(HttpRequest request) { var cookieCollection = new CookieCollection(); HttpCookieCollection cookies = request.Cookies; for (int i = 0; i < cookies.Count; i++) { var cookie = new Cookie(cookies[i].Name, cookies[i].Value); cookieCollection.Add(cookie); } return cookieCollection; }
public static CookieContainer Deserialize(Uri uri, Stream stream) { var container = new CookieContainer(); var serializer = new DataContractSerializer(typeof (IEnumerable<Cookie>)); var cookies = (IEnumerable<Cookie>) serializer.ReadObject(stream); var cookieCollection = new CookieCollection(); foreach (Cookie cookie in cookies) { cookieCollection.Add(cookie); } container.Add(uri, cookieCollection); return container; }
/// <summary> /// These are the cookies that allow us to remain in a logged in state. /// This is required because Code Project requires you to be signed in /// to download files. I got help in learning how to use these from /// http://blogs.msdn.com/dgorti/archive/2005/08/16/452347.aspx /// </summary> private void ParseCookies(string cookieString) { try { string[] token = new String[] { "; " }; String[] cookieArray = cookieString.Split(token, StringSplitOptions.RemoveEmptyEntries); m_cookies = new CookieCollection(); for (Int32 i = 0; i < cookieArray.Length; i++) { char[] tokenChar = new Char[] { '=' }; String[] cookie = cookieArray[i].Split(tokenChar, 2, StringSplitOptions.RemoveEmptyEntries); if (cookie.Length == 2) m_cookies.Add(new Cookie(cookie[0], cookie[1])); else if (cookie.Length == 1) m_cookies.Add(new Cookie(cookie[0], String.Empty)); } } catch (Exception e) { string errMsg = "Unable to parse page cookies: " + e.Message; throw new Exception(errMsg, e); } }
public void SetCookieCollectionToNullCleansCollection() { var storage = new PersistentCookieStorage(this.engine); var collection = new CookieCollection(); collection.Add(new Cookie { Name = "test", Expired = false, Expires = DateTime.Now.AddDays(1) }); storage.Cookies = collection; storage.Cookies = null; Assert.That(storage.Cookies, Is.Empty); }
/// <summary> /// Deserialize cookie after aothorization /// </summary> /// <param name="stream"></param> /// <param name="uri"></param> /// <returns></returns> public static CookieContainer Deserialize(Stream stream, Uri uri) { List<Cookie> cookies = new List<Cookie>(); CookieContainer container = new CookieContainer(); DataContractSerializer formatter = new DataContractSerializer(typeof(List<Cookie>)); cookies = (List<Cookie>)formatter.ReadObject(stream); CookieCollection cookieco = new CookieCollection(); foreach (Cookie cookie in cookies) { cookieco.Add(cookie); } container.Add(uri, cookieco); return container; }
private static CookieCollection GetAllCookieCollection() { string[] cookies = HtmlPage.Document.Cookies.Split(';'); CookieCollection cookieCollection = new CookieCollection(); foreach (string cookie in cookies) { string[] cookieParts = cookie.Split('='); if (cookieParts.Count() >= 1) { cookieCollection.Add(new Cookie(cookieParts[0].Trim(), cookieParts[1].Trim())); } } return cookieCollection; }
public static CookieCollection GetCookies() { CookieCollection cookies = new CookieCollection(); delegate { foreach (string str in HtmlPage.Document.Cookies.Split(new char[] { ';' })) { string[] strArray2 = str.Split(new char[] { '=' }); if (strArray2.Length == 2) { Cookie cookie = new Cookie(strArray2[0].Trim(), strArray2[1].Trim()); cookies.Add(cookie); } } }.OnUIThread(); return cookies; }
protected override CookieImportResult ProtectedGetCookies(Uri targetUrl) { if (IsAvailable == false) return new CookieImportResult(null, CookieImportState.Unavailable); try { var cookies = new CookieCollection(); var query = string.Format("{0} {1} ORDER BY expiry", SELECT_QUERY, MakeWhere(targetUrl)); foreach (var item in LookupCookies(SourceInfo.CookiePath, query, DataToCookie)) cookies.Add(item); return new CookieImportResult(cookies, CookieImportState.Success); } catch (CookieImportException ex) { TraceError(this, "取得に失敗しました。", ex.ToString()); return new CookieImportResult(null, ex.Result); } }
public static CookieContainer Deserialize(Uri uri, Stream stream) { var container = new CookieContainer(); var serializer = new DataContractSerializer(typeof (IEnumerable<Cookie>)); var cookies = (IEnumerable<Cookie>) serializer.ReadObject(stream); var cookieCollection = new CookieCollection(); // TODO: HUGE HACK. For some reason Windows Phone does not use the Domain Key on a cookie, but only the domain when making requests. // Windows 8 won't break on it, but Windows Phone will, since the Domain Key and Domain are different on SA. // We need to move this code to a more common place. foreach (Cookie cookie in cookies) { var fixedCookie = new Cookie(cookie.Name, cookie.Value, "/", ".somethingawful.com"); cookieCollection.Add(fixedCookie); } container.Add(uri, cookieCollection); return container; }
private CookieContainer prepareCookies(CookieContainer oldCookies) { CookieCollection cookies = oldCookies.GetCookies(new Uri("http://trade.plus500.com")); CookieCollection newCookies = new CookieCollection(); foreach (Cookie cookie in cookies) { if (!cookie.Name.Contains("Password")) { newCookies.Add(cookie); } } CookieContainer cookieContainer = new CookieContainer(); cookieContainer.Add(newCookies); return cookieContainer; }
protected override CookieImportResult ProtectedGetCookies(Uri targetUrl) { string cookiesText; var hResult = Win32Api.GetCookiesFromIE(out cookiesText, targetUrl, null); if (cookiesText == null) return new CookieImportResult(null, CookieImportState.AccessError); try { var cookies = new CookieCollection(); foreach (var item in ParseCookies(cookiesText, targetUrl)) cookies.Add(item); return new CookieImportResult(cookies, CookieImportState.Success); } catch (CookieImportException ex) { TraceError(this, "Cookie読み込みに失敗。", ex.ToString()); return new CookieImportResult(null, ex.Result); } }
protected override CookieImportResult ProtectedGetCookies(Uri targetUrl) { if (IsAvailable == false) return new CookieImportResult(null, CookieImportState.Unavailable); try { //IEのバージョンによって使えるAPIに違いがあるため、分岐させる。 //APIは呼び出す環境で違いがある。IE8-x-IE10 on x64の時にはx86の子プロセスで取得させる。 // [環境ごとの違い一覧] // * x<IE8 on x86,x64 使用不可 // * IE8<=x<IE11 on x86 使用可能 // * IE8<=x<IE11 on x64 使用不可 // * IE11<=x on x86,x64 使用可能 string cookiesText; var ieVersion = Win32Api.GetIEVersion(); if(ieVersion == null) return new CookieImportResult(null, CookieImportState.AccessError); if (ieVersion.Major >= 11 || ieVersion.Major >= 8 && Environment.Is64BitProcess == false) cookiesText = InternalGetCookiesWinApi(targetUrl, null); else if (ieVersion.Major >= 8 && Environment.OSVersion.Platform == PlatformID.Win32NT) cookiesText = InternalGetCookiesWinApiOnProxy(targetUrl, null); else cookiesText = null; if (cookiesText != null) { var cookies = new CookieCollection(); foreach (var item in ParseCookies(cookiesText, targetUrl)) cookies.Add(item); return new CookieImportResult(cookies, CookieImportState.Success); } else return new CookieImportResult(null, CookieImportState.AccessError); } catch (CookieImportException ex) { TraceError(this, "Cookie読み込みに失敗。", ex.ToString()); return new CookieImportResult(null, ex.Result); } }
public static void RemoveCookie(this CookieContainer cookieContainer, string name) { Contract.Requires<ArgumentNullException>(cookieContainer != null); Contract.Requires<ArgumentNullException>(name != null); var lists = GetCookieLists(cookieContainer); foreach (var list in lists) { Contract.Assume(list != null); var keys = list.Keys.Cast<object>().ToArray(); foreach (var key in keys) { if (key == null) continue; var cookies = (CookieCollection)list[key]; if (cookies == null) continue; var hasCookie = cookies.Cast<Cookie>().Any(t => t.Name == name); if (hasCookie == false) continue; var newCookies = new CookieCollection(); foreach (Cookie cookie in cookies) { if (cookie != null) { if (cookie.Name != name) { newCookies.Add(cookie); } } } list[key] = newCookies; } } }
public static string OpenLongTimeUrl(string TargetURL, string RefURL, string Body, string Method, System.Net.CookieCollection BrowCookie, Encoding ContactEncoding, bool AllowRedirect = false, bool KeepAlive = true, string ContentType = "application/json;charset=UTF-8", string authorization = "") { //System.Net.ServicePointManager.MaxServicePoints=20; System.Net.ServicePointManager.DefaultConnectionLimit = 500; System.Net.ServicePointManager.SetTcpKeepAlive(true, 15000, 15000); //HttpWebRequest LoginPage = null; // GetHttpWebResponseNoRedirect(TargetURL,"","",out LoginPage); WebRequest LoginPage = HttpWebRequest.Create(TargetURL); ((HttpWebRequest)LoginPage).AllowAutoRedirect = AllowRedirect; ((HttpWebRequest)LoginPage).KeepAlive = KeepAlive; //SetHeaderValue(((HttpWebRequest)LoginPage).Headers, "Connection", "Keep-Alive"); ((HttpWebRequest)LoginPage).Timeout = 30000; ((HttpWebRequest)LoginPage).Credentials = CredentialCache.DefaultCredentials; if (authorization != "") { LoginPage.Headers.Add("Authorization", authorization); } LoginPage.Method = Method; if (TargetURL.ToLower().StartsWith("https")) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3; //System.Net.ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult; ((HttpWebRequest)LoginPage).ProtocolVersion = System.Net.HttpVersion.Version11; } switch (Method) { case "GET": // ((HttpWebRequest)LoginPage).Accept = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-powerpoint, application/msword, application/vnd.ms-excel,application/json, text/plain, */*"; ((HttpWebRequest)LoginPage).Accept = "*/*"; ((HttpWebRequest)LoginPage).UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36 OPR/52.0.2871.40"; LoginPage.Headers.Add("Accept-Encoding", "gzip, deflate,br"); LoginPage.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9"); ((HttpWebRequest)LoginPage).CookieContainer = new CookieContainer(); ((HttpWebRequest)LoginPage).CookieContainer.Add(BrowCookie); //((HttpWebRequest)LoginPage).Connection = "KeepAlive,Close"; ((HttpWebRequest)LoginPage).Referer = RefURL; break; case "POST": ((HttpWebRequest)LoginPage).Accept = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-powerpoint, application/msword, application/vnd.ms-excel,application/json, text/plain, */*"; ((HttpWebRequest)LoginPage).Referer = RefURL; ((HttpWebRequest)LoginPage).UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)"; LoginPage.Headers.Add("Accept-Encoding", "gzip, deflate,br"); LoginPage.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9"); ((HttpWebRequest)LoginPage).CookieContainer = new CookieContainer(); ((HttpWebRequest)LoginPage).CookieContainer.Add(BrowCookie); ((HttpWebRequest)LoginPage).ContentType = ContentType; //((HttpWebRequest)LoginPage).ServicePoint.Expect100Continue = true; //((HttpWebRequest)LoginPage).Connection = "KeepAlive"; if (((HttpWebRequest)LoginPage).Referer != null) { LoginPage.Headers.Add("Origin", ((HttpWebRequest)LoginPage).Referer.Substring(0, ((HttpWebRequest)LoginPage).Referer.Length - 1)); } if (Body != "") { Stream bodys = LoginPage.GetRequestStream(); byte[] text = ContactEncoding.GetBytes(Body); bodys.Write(text, 0, text.Length); bodys.Flush(); bodys.Close(); } break; case "OPTIONS": // ((HttpWebRequest)LoginPage).Accept = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-powerpoint, application/msword, application/vnd.ms-excel,application/json, text/plain, */*"; ((HttpWebRequest)LoginPage).Accept = "*/*"; ((HttpWebRequest)LoginPage).UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36 OPR/52.0.2871.40"; LoginPage.Headers.Add("Accept-Encoding", "gzip, deflate,br"); LoginPage.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9"); ((HttpWebRequest)LoginPage).CookieContainer = new CookieContainer(); ((HttpWebRequest)LoginPage).CookieContainer.Add(BrowCookie); //((HttpWebRequest)LoginPage).Connection = "KeepAlive"; ((HttpWebRequest)LoginPage).Referer = RefURL; LoginPage.Headers.Add("Origin", RefURL); break; default: break; } ((HttpWebRequest)LoginPage).KeepAlive = true; SetHeaderValue(((HttpWebRequest)LoginPage).Headers, "Connection", "Keep-Alive"); LoginPage.Timeout = 30000; if (RefURL.ToLower().StartsWith("https")) { //System.Net.ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult; ((HttpWebRequest)LoginPage).ProtocolVersion = System.Net.HttpVersion.Version11; } //System.GC.Collect(); System.Threading.Thread.Sleep(100); HttpWebResponse LoginPage_Return = null; try { //CurrentUrl = "正在下载" + TargetURL; //System.GC.Collect(); // NetFramework.Console.WriteLine("下载URL" + LoginPage.RequestUri.AbsoluteUri + Environment.NewLine); LoginPage_Return = (HttpWebResponse)LoginPage.GetResponse(); //CurrentUrl = "已下载" + TargetURL; if (((HttpWebResponse)LoginPage_Return).Headers["Set-Cookie"] != null) { string Start = LoginPage.RequestUri.Host.Substring(0, LoginPage.RequestUri.Host.LastIndexOf(".")); string Host = LoginPage.RequestUri.Host.Substring(LoginPage.RequestUri.Host.LastIndexOf(".")); foreach (Cookie cookieitem in ((HttpWebResponse)LoginPage_Return).Cookies) { string[] SplitDomain = cookieitem.Domain.Split((".").ToCharArray(), StringSplitOptions.RemoveEmptyEntries); Int32 Length = SplitDomain.Length; cookieitem.Domain = "." + SplitDomain[Length - 2] + "." + SplitDomain[Length - 1]; cookieitem.Expires = cookieitem.Expires == null?DateTime.Now.AddHours(168) : cookieitem.Expires.AddHours(168); BrowCookie.Add(cookieitem); } //CookieContainer NC = new CookieContainer(); //NC.SetCookies(((HttpWebResponse)LoginPage_Return).ResponseUri, ((HttpWebResponse)LoginPage_Return).Headers["Set-Cookie"]); //BrowCookie.Add(NC.GetCookies(((HttpWebResponse)LoginPage_Return).ResponseUri)); // Host = Start.Substring(Start.LastIndexOf(".")) + Host; // AddCookieWithCookieHead(tmpcookie, ((HttpWebResponse)LoginPage_Return).Headers["Set-Cookie"].Replace("Secure,", ""), Host); } } catch (Exception AnyError) { LoginPage = null; System.GC.Collect(); //NetFramework.Console.WriteLine("网址打开失败" + TargetURL, false); //NetFramework.Console.WriteLine("网址打开失败" + AnyError.Message, false); //NetFramework.Console.WriteLine("网址打开失败" + AnyError.StackTrace, false); return(AnyError.Message); } string responseBody = string.Empty; try { if (LoginPage_Return.ContentEncoding.ToLower().Contains("gzip")) { using (GZipStream stream = new GZipStream(LoginPage_Return.GetResponseStream(), CompressionMode.Decompress)) { using (StreamReader reader = new StreamReader(stream, ContactEncoding)) { responseBody = reader.ReadToEnd(); stream.Close(); } } } else if (LoginPage_Return.ContentEncoding.ToLower().Contains("deflate")) { using (DeflateStream stream = new DeflateStream(LoginPage_Return.GetResponseStream(), CompressionMode.Decompress)) { using (StreamReader reader = new StreamReader(stream, ContactEncoding)) { responseBody = reader.ReadToEnd(); stream.Close(); } } } else if (LoginPage_Return.ContentEncoding.ToLower().Contains("br")) { using (Brotli.BrotliStream stream = new Brotli.BrotliStream(LoginPage_Return.GetResponseStream(), CompressionMode.Decompress)) { using (StreamReader reader = new StreamReader(stream, ContactEncoding)) { responseBody = reader.ReadToEnd(); stream.Close(); } } } else { using (Stream stream = LoginPage_Return.GetResponseStream()) { using (StreamReader reader = new StreamReader(stream, ContactEncoding)) { responseBody = reader.ReadToEnd(); stream.Close(); } } } } catch (Exception AnyError) { LoginPage.Abort(); LoginPage = null; //System.GC.Collect(); //NetFramework.Console.WriteLine("网址打开失败" + TargetURL, true); //NetFramework.Console.WriteLine("网址打开失败" + AnyError.Message, true); //NetFramework.Console.WriteLine("网址打开失败" + AnyError.StackTrace, true); return(AnyError.Message); } LoginPage.Abort(); // NetFramework.Console.WriteLine("下载完成" + LoginPage_Return.ResponseUri.AbsoluteUri + Environment.NewLine); LoginPage_Return.Close(); LoginPage_Return = null; LoginPage = null; System.GC.Collect(); return(responseBody); }
private void SetCookie(string header) { Cookie cookie = null; CookieParser cookieParser = new CookieParser(header); string name; string val; while (cookieParser.GetNextNameValue(out name, out val)) { if ((name == null || name == string.Empty) && cookie == null) { continue; } if (cookie == null) { cookie = new Cookie(name, val); continue; } name = name.ToUpper(); switch (name) { case "COMMENT": if (cookie.Comment == null) { cookie.Comment = val; } break; case "COMMENTURL": if (cookie.CommentUri == null) { cookie.CommentUri = new Uri(val); } break; case "DISCARD": cookie.Discard = true; break; case "DOMAIN": if (cookie.Domain == string.Empty) { cookie.Domain = val; } break; case "HTTPONLY": cookie.HttpOnly = true; break; case "MAX-AGE": if (cookie.Expires == DateTime.MinValue) { try { cookie.Expires = cookie.TimeStamp.AddSeconds(uint.Parse(val)); } catch { } } break; case "EXPIRES": if (!(cookie.Expires != DateTime.MinValue)) { cookie.Expires = TryParseCookieExpires(val); } break; case "PATH": cookie.Path = val; break; case "PORT": if (cookie.Port == null) { cookie.Port = val; } break; case "SECURE": cookie.Secure = true; break; case "VERSION": try { cookie.Version = (int)uint.Parse(val); } catch { } break; } } if (cookie != null) { if (cookieCollection == null) { cookieCollection = new CookieCollection(); } if (cookie.Domain == string.Empty) { cookie.Domain = uri.Host; } cookieCollection.Add(cookie); if (cookie_container != null) { cookie_container.Add(uri, cookie); } } }
internal void AddHeader(string header) { int colon = header.IndexOf(':'); if (colon == -1 || colon == 0) { context.ErrorMessage = "Bad Request"; context.ErrorStatus = 400; return; } string name = header.Substring(0, colon).Trim(); string val = header.Substring(colon + 1).Trim(); string lower = name.ToLower(CultureInfo.InvariantCulture); headers.SetInternal(name, val); switch (lower) { case "accept-language": user_languages = val.Split(','); // yes, only split with a ',' break; case "accept": accept_types = val.Split(','); // yes, only split with a ',' break; case "content-length": try { //TODO: max. content_length? content_length = Int64.Parse(val.Trim()); if (content_length < 0) { context.ErrorMessage = "Invalid Content-Length."; } cl_set = true; } catch { context.ErrorMessage = "Invalid Content-Length."; } break; case "referer": try { referrer = new Uri(val); } catch { referrer = new Uri("http://someone.is.screwing.with.the.headers.com/"); } break; case "cookie": if (cookies == null) { cookies = new CookieCollection(); } string[] cookieStrings = val.Split(new char[] { ',', ';' }); Cookie current = null; int version = 0; foreach (string cookieString in cookieStrings) { string str = cookieString.Trim(); if (str.Length == 0) { continue; } if (str.StartsWith("$Version")) { version = Int32.Parse(Unquote(str.Substring(str.IndexOf('=') + 1))); } else if (str.StartsWith("$Path")) { if (current != null) { current.Path = str.Substring(str.IndexOf('=') + 1).Trim(); } } else if (str.StartsWith("$Domain")) { if (current != null) { current.Domain = str.Substring(str.IndexOf('=') + 1).Trim(); } } else if (str.StartsWith("$Port")) { if (current != null) { current.Port = str.Substring(str.IndexOf('=') + 1).Trim(); } } else { if (current != null) { cookies.Add(current); } current = new Cookie(); int idx = str.IndexOf('='); if (idx > 0) { current.Name = str.Substring(0, idx).Trim(); current.Value = str.Substring(idx + 1).Trim(); } else { current.Name = str.Trim(); current.Value = String.Empty; } current.Version = version; } } if (current != null) { cookies.Add(current); } break; } }
public CookieCollection GetCookies(Uri uri) { if (uri == null) { throw new ArgumentNullException("uri"); } CheckExpiration(); CookieCollection coll = new CookieCollection(); if (cookies == null) { return(coll); } foreach (Cookie cookie in cookies) { string domain = cookie.Domain; string host = uri.Host; if (!CheckDomain(domain, host)) { continue; } if (cookie.Port != "" && cookie.Ports != null && uri.Port != -1) { if (Array.IndexOf(cookie.Ports, uri.Port) == -1) { continue; } } string path = cookie.Path; string uripath = uri.AbsolutePath; if (path != "" && path != "/") { if (uripath != path) { if (!uripath.StartsWith(path)) { continue; } if (path [path.Length - 1] != '/' && uripath.Length > path.Length && uripath [path.Length] != '/') { continue; } } } if (cookie.Secure && uri.Scheme != "https") { continue; } coll.Add(cookie); } coll.SortByPath(); return(coll); }
void SetCookie(string header) { string name, val; Cookie cookie = null; CookieParser parser = new CookieParser(header); while (parser.GetNextNameValue(out name, out val)) { if ((name == null || name == "") && cookie == null) { continue; } if (cookie == null) { cookie = new Cookie(name, val); continue; } name = name.ToUpper(); switch (name) { case "COMMENT": if (cookie.Comment == null) { cookie.Comment = val; } break; case "COMMENTURL": if (cookie.CommentUri == null) { cookie.CommentUri = new Uri(val); } break; case "DISCARD": cookie.Discard = true; break; case "DOMAIN": if (cookie.Domain == "") { cookie.Domain = val; } break; case "MAX-AGE": // RFC Style Set-Cookie2 if (cookie.Expires == DateTime.MinValue) { try { cookie.Expires = cookie.TimeStamp.AddSeconds(UInt32.Parse(val)); } catch {} } break; case "EXPIRES": // Netscape Style Set-Cookie if (cookie.Expires != DateTime.MinValue) { break; } cookie.Expires = TryParseCookieExpires(val); break; case "PATH": cookie.Path = val; break; case "PORT": if (cookie.Port == null) { cookie.Port = val; } break; case "SECURE": cookie.Secure = true; break; case "VERSION": try { cookie.Version = (int)UInt32.Parse(val); } catch {} break; } } if (cookieCollection == null) { cookieCollection = new CookieCollection(); } if (cookie.Domain == "") { cookie.Domain = uri.Host; } cookieCollection.Add(cookie); if (cookie_container != null) { cookie_container.Add(uri, cookie); } }
/// <inheritdoc /> public void Add(Cookie cookie) => _cookieCollection.Add(cookie);
public CookieCollection GetCookies(Uri uri) { if (uri == null) { throw new ArgumentNullException("uri"); } CheckExpiration(); CookieCollection2 coll = new CookieCollection2(); if (cookies == null) { return(new CookieCollection()); } foreach (Cookie cookie in cookies) { string domain = cookie.Domain; if (cookie.Version == 1) { if (!CheckDomain_RFC2109(domain, uri.Host)) { continue; } } else { if (!CheckDomain(domain, uri.Host, false)) { continue; } } string path = cookie.Path; string uripath = uri.AbsolutePath; if (path != "" && path != "/") { if (uripath != path) { if (!uripath.StartsWith(path)) { continue; } if (path [path.Length - 1] != '/' && uripath.Length > path.Length && uripath [path.Length] != '/') { continue; } } } if (cookie.Secure && uri.Scheme != "https") { continue; } coll.Add(cookie); } coll.Sort(); var result = new CookieCollection(); foreach (var item in coll.List) { result.Add(item); } return(result); }
internal void AddHeader(string header) { var colon = header.IndexOf(':'); if (colon == -1 || colon == 0) { _context.ErrorMessage = "Bad Request"; _context.ErrorStatus = 400; return; } var name = header.Substring(0, colon).Trim(); var val = header.Substring(colon + 1).Trim(); var lower = name.ToLowerInvariant(); Headers.Set(name, val); switch (lower) { case "accept-language": UserLanguages = val.Split(','); // yes, only split with a ',' break; case "accept": AcceptTypes = val.Split(','); // yes, only split with a ',' break; case "content-length": try { //TODO: max. content_length? ContentLength64 = long.Parse(val.Trim()); if (ContentLength64 < 0) { _context.ErrorMessage = "Invalid Content-Length."; } _clSet = true; } catch { _context.ErrorMessage = "Invalid Content-Length."; } break; case "referer": try { UrlReferrer = new Uri(val); } catch { UrlReferrer = new Uri("http://someone.is.screwing.with.the.headers.com/"); } break; case "cookie": if (_cookies == null) { _cookies = new CookieCollection(); } var cookieStrings = val.Split(',', ';'); Cookie current = null; var version = 0; foreach (var cookieString in cookieStrings) { var str = cookieString.Trim(); if (str.Length == 0) { continue; } if (str.StartsWith("$Version")) { version = int.Parse(Unquote(str.Substring(str.IndexOf('=') + 1))); } else if (str.StartsWith("$Path")) { if (current != null) { current.Path = str.Substring(str.IndexOf('=') + 1).Trim(); } } else if (str.StartsWith("$Domain")) { if (current != null) { current.Domain = str.Substring(str.IndexOf('=') + 1).Trim(); } } else if (str.StartsWith("$Port")) { if (current != null) { current.Port = str.Substring(str.IndexOf('=') + 1).Trim(); } } else { if (current != null) { _cookies.Add(current); } current = new Cookie(); var idx = str.IndexOf('='); if (idx > 0) { current.Name = str.Substring(0, idx).Trim(); current.Value = str.Substring(idx + 1).Trim(); } else { current.Name = str.Trim(); current.Value = string.Empty; } current.Version = version; } } if (current != null) { _cookies.Add(current); } break; } }
internal void AddHeader(string header) { int colon = header.IndexOf(':'); if (colon == -1 || colon == 0) { _context.ErrorMessage = HttpStatusDescription.Get(400); _context.ErrorStatus = 400; return; } string name = header.Substring(0, colon).Trim(); string val = header.Substring(colon + 1).Trim(); string lower = name.ToLower(CultureInfo.InvariantCulture); _headers.Set(name, val); switch (lower) { case "accept-language": _userLanguages = val.Split(','); // yes, only split with a ',' break; case "accept": _acceptTypes = val.Split(','); // yes, only split with a ',' break; case "content-length": try { _contentLength = long.Parse(val.Trim()); if (_contentLength < 0) { _context.ErrorMessage = "Invalid Content-Length."; } _clSet = true; } catch { _context.ErrorMessage = "Invalid Content-Length."; } break; case "referer": try { _referrer = new Uri(val); } catch { _referrer = null; } break; case "cookie": if (_cookies == null) { _cookies = new CookieCollection(); } string[] cookieStrings = val.Split(new char[] { ',', ';' }); Cookie current = null; int version = 0; foreach (string cookieString in cookieStrings) { string str = cookieString.Trim(); if (str.Length == 0) { continue; } if (str.StartsWith("$Version")) { version = Int32.Parse(Unquote(str.Substring(str.IndexOf('=') + 1))); } else if (str.StartsWith("$Path")) { if (current != null) { current.Path = str.Substring(str.IndexOf('=') + 1).Trim(); } } else if (str.StartsWith("$Domain")) { if (current != null) { current.Domain = str.Substring(str.IndexOf('=') + 1).Trim(); } } else if (str.StartsWith("$Port")) { if (current != null) { current.Port = str.Substring(str.IndexOf('=') + 1).Trim(); } } else { if (current != null) { _cookies.Add(current); } current = new Cookie(); int idx = str.IndexOf('='); if (idx > 0) { current.Name = str.Substring(0, idx).Trim(); current.Value = str.Substring(idx + 1).Trim(); } else { current.Name = str.Trim(); current.Value = String.Empty; } current.Version = version; } } if (current != null) { _cookies.Add(current); } break; } }