Ejemplo n.º 1
0
        public async Task WebSocketOptionsAreApplied()
        {
            using (StartLog(out var loggerFactory, $"{nameof(WebSocketOptionsAreApplied)}"))
            {
                // System.Net has a TransportType type which means we need to fully-qualify this rather than 'use' the namespace
                var cookieJar = new System.Net.CookieContainer();
                cookieJar.Add(new System.Net.Cookie("Foo", "Bar", "/", new Uri(_serverFixture.Url).Host));

                var hubConnection = new HubConnectionBuilder()
                                    .WithUrl(_serverFixture.Url + "/default")
                                    .WithTransport(TransportType.WebSockets)
                                    .WithLoggerFactory(loggerFactory)
                                    .WithWebSocketOptions(options => options.Cookies = cookieJar)
                                    .Build();
                try
                {
                    await hubConnection.StartAsync().OrTimeout();

                    var cookieValue = await hubConnection.InvokeAsync <string>(nameof(TestHub.GetCookieValue), new object[] { "Foo" }).OrTimeout();

                    Assert.Equal("Bar", cookieValue);
                }
                catch (Exception ex)
                {
                    loggerFactory.CreateLogger <HubConnectionTests>().LogError(ex, "{ExceptionType} from test", ex.GetType().FullName);
                    throw;
                }
                finally
                {
                    await hubConnection.DisposeAsync().OrTimeout();
                }
            }
        }
Ejemplo n.º 2
0
		private System.Net.CookieContainer ParseCookieSettings(string line)
		{
			System.Net.CookieContainer container = new System.Net.CookieContainer();

			// クッキー情報の前についているよくわからないヘッダー情報を取り除く
			// 対象:
			//  \\xと2桁の16進数値
			//  \\\\
			//  \がない場合の先頭1文字
			string matchPattern = "^(\\\\x[0-9a-fA-F]{2})|^(\\\\\\\\)|^(.)|[\"()]";
			System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(matchPattern, System.Text.RegularExpressions.RegexOptions.Compiled);

			string[] blocks = line.Split(new string[] { "\\0\\0\\0" }, StringSplitOptions.RemoveEmptyEntries);
			foreach (string block in blocks) {
				if (block.Contains("=") && block.Contains("domain")) {
					string header = reg.Replace(block, "");
					System.Net.Cookie cookie = ParseCookie(header);
					if (cookie != null) {
						try {
							container.Add(cookie);
						} catch(Exception ex) {
							CookieGetter.Exceptions.Enqueue(ex);
						}
					}
				}
			}

			return container;
		}
Ejemplo n.º 3
0
        public async Task WebSocketOptionsAreApplied()
        {
            using (StartServer <Startup>(out var server))
            {
                // System.Net has a HttpTransportType type which means we need to fully-qualify this rather than 'use' the namespace
                var cookieJar = new System.Net.CookieContainer();
                cookieJar.Add(new System.Net.Cookie("Foo", "Bar", "/", new Uri(server.Url).Host));

                var hubConnection = new HubConnectionBuilder()
                                    .WithLoggerFactory(LoggerFactory)
                                    .WithUrl(server.Url + "/default", HttpTransportType.WebSockets, options =>
                {
                    options.WebSocketConfiguration = o => o.Cookies = cookieJar;
                })
                                    .Build();
                try
                {
                    await hubConnection.StartAsync().OrTimeout();

                    var cookieValue = await hubConnection.InvokeAsync <string>(nameof(TestHub.GetCookieValue), "Foo").OrTimeout();

                    Assert.Equal("Bar", cookieValue);
                }
                catch (Exception ex)
                {
                    LoggerFactory.CreateLogger <HubConnectionTests>().LogError(ex, "{ExceptionType} from test", ex.GetType().FullName);
                    throw;
                }
                finally
                {
                    await hubConnection.DisposeAsync().OrTimeout();
                }
            }
        }
Ejemplo n.º 4
0
        public override void SendNoWait()
        {
            var cookie = new System.Net.CookieContainer();

            cookie.Add(new Uri(Endpoint.IP), Message.Cookie);
            HttpClient.SendAsync(Message.Verb, Endpoint.IP, Message, HttpClient.CONTENTTYPE, cookie);
        }
Ejemplo n.º 5
0
        public static async Task Main(string[] args)
        {
            var cookie = new System.Net.CookieContainer();

            using (var handler = new HttpClientHandler()
            {
                CookieContainer = cookie
            })
                using (var client = new HttpClient(handler))
                {
                    client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36");
                    client.BaseAddress = new Uri($"{args[1]}/index.php");

                    cookie.Add(new Uri(args[1]), new System.Net.Cookie("id", args[2]));

                    foreach (var data in GetData())
                    {
                        foreach (var key in data.Keys)
                        {
                            Console.WriteLine($"{DateTime.Now}: {data.Dir} - '{key}'. Start.");
                            await LoadVideo(client, key, data.Dir);

                            Console.WriteLine($"{DateTime.Now}: {data.Dir} - '{key}'. End.");
                            await Task.Delay(10000);
                        }
                    }
                }
        }
Ejemplo n.º 6
0
 private void ReadCookies(System.Net.WebResponse r)
 {
     System.Net.HttpWebResponse response = r as System.Net.HttpWebResponse;
     if (response != null)
     {
         System.Net.CookieCollection cookies = response.Cookies;
         container.Add(cookies);
     }
 }
Ejemplo n.º 7
0
        /// <summary>
        /// クッキーコンテナにクッキーを追加する
        /// domainが.hal.fscs.jpなどだと http://hal.fscs.jp でクッキーが有効にならないので.ありとなし両方指定する
        /// </summary>
        public static void AddCookieToContainer(System.Net.CookieContainer container, System.Net.Cookie cookie)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }

            if (cookie == null)
            {
                throw new ArgumentNullException("cookie");
            }

            container.Add(cookie);
            if (cookie.Domain.StartsWith("."))
            {
                container.Add(new System.Net.Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain.Substring(1)));
            }
        }
Ejemplo n.º 8
0
        public async override Task <Message> SendAsync()
        {
            var cookie = new System.Net.CookieContainer();

            cookie.Add(new Uri(Endpoint.IP), Message.Cookie);
            return(await HttpClient.SendAsync(Message.Verb, Endpoint.IP, Message, HttpClient.CONTENTTYPE, cookie).ContinueWith <Message>(m => {
                m.Result.Context = Message.Context;
                return m.Result;
            }));
        }
Ejemplo n.º 9
0
        public override void Send()
        {
            var cookie = new System.Net.CookieContainer();

            cookie.Add(new Uri(Endpoint.IP), Message.Cookie);
            var task = HttpClient.SendAsync(Message.Verb, Endpoint.IP, Message, HttpClient.CONTENTTYPE, cookie);

            task.Wait();
            task.Result.Context = Message.Context;
        }
 private void LoadCookie()
 {
     // cannot deserialize directly to cookiecontainer unfortunately
     if (!string.IsNullOrEmpty(Settings.APICookie))
     {
         var collection = JsonConvert.DeserializeObject <List <System.Net.Cookie> >(Settings.APICookie);
         foreach (var cookie in collection)
         {
             cookieContainer.Add(cookie);
         }
     }
 }
Ejemplo n.º 11
0
        private void label1_Click(object sender, EventArgs e)
        {
            var cookies = new System.Net.CookieContainer()
            {
                Capacity = 100
            };

            cookies.Add(new System.Net.Cookie("asdf", "adsfsdf", "/", "baidu.com"));



            this.contextMenuStrip1.Show(label1, this.label1.Location);
        }
Ejemplo n.º 12
0
		/// <summary>
		/// ログインした人のアカウント情報を取得します
		/// </summary>
		/// <param name="cookieGetter"></param>
		/// <returns>ログインが完了していない場合はNullを返します</returns>
		public static AccountInfomation GetMyAccountInfomation(CookieGetterSharp.ICookieGetter cookieGetter)
		{
			if (cookieGetter.Status.IsAvailable) {
				try {
					System.Net.CookieContainer container = new System.Net.CookieContainer();
					container.Add(cookieGetter.GetCookieCollection(new Uri("http://live.nicovideo.jp/")));
					return GetMyAccountInfomation(container);
				} catch { 
				}
			}

			return null;
		}
Ejemplo n.º 13
0
        public void LoadCookies()
        {
            var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

            using (var s = System.IO.File.OpenRead(cookie_file))
            {
                var v = formatter.Deserialize(s);
                var c = v as System.Net.CookieContainer;
                foreach (var item in c.GetCookies(new Uri(config.base_url)))
                {
                    var d = item as System.Net.Cookie;
                    cookies.Add(d);
                }
            }
        }
Ejemplo n.º 14
0
        private void UpdateCookies()
        {
            var CookCont = new System.Net.CookieContainer();

            foreach (var SCookie in this.Cookies)
            {
                CookCont.Add(new System.Net.Cookie()
                {
                    Domain = ".tranimeizle.net",
                    Name   = SCookie.Name,
                    Value  = SCookie.Value
                });
            }

            this._HttpClient.CookieContainer = CookCont;
        }
Ejemplo n.º 15
0
        internal static async Task <string> DownloadStringAsyncCookie(Int32 TimeOut, string UserAgent, string[][] RequestHeader, Uri DownloadUrl, string cookieString)
        {
            try
            {
                //Set the cookies to container
                System.Net.CookieContainer cookieContainer = new System.Net.CookieContainer();
                foreach (string cookieSingle in cookieString.Split(','))
                {
                    Match cookieMatch = Regex.Match(cookieSingle, "(.+?)=(.+?);");
                    if (cookieMatch.Captures.Count > 0)
                    {
                        string CookieName  = cookieMatch.Groups[1].ToString().Replace(" ", String.Empty);
                        string CookieValue = cookieMatch.Groups[2].ToString().Replace(" ", String.Empty);
                        cookieContainer.Add(DownloadUrl, new System.Net.Cookie(CookieName, CookieValue));
                    }
                }

                using (System.Net.Http.HttpClientHandler httpHandler = new System.Net.Http.HttpClientHandler())
                {
                    httpHandler.CookieContainer = cookieContainer;
                    using (System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient(httpHandler))
                    {
                        httpClient.Timeout = TimeSpan.FromMilliseconds(TimeOut);
                        httpClient.DefaultRequestHeaders.Add("User-Agent", UserAgent);
                        httpClient.DefaultRequestHeaders.Add("Cache-Control", "no-cache, no-store");
                        if (RequestHeader != null)
                        {
                            foreach (String[] StringArray in RequestHeader)
                            {
                                httpClient.DefaultRequestHeaders.Add(StringArray[0], StringArray[1]);
                            }
                        }

                        string ConnectTask = await httpClient.GetStringAsync(DownloadUrl);

                        Debug.WriteLine("DownloadStringAsyncCookie succeeded for url: " + DownloadUrl + " / " + ConnectTask.Length + "bytes");
                        return(ConnectTask);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("DownloadStringAsyncCookie exception for url: " + DownloadUrl + " / " + ex.Message);
                return(String.Empty);
            }
        }
Ejemplo n.º 16
0
        private async Task <HttpResponseMessage> GetAsync(Uri uri)
        {
            var baseAddress     = new Uri(MEK_ENDPOINT_URL);
            var cookieContainer = new System.Net.CookieContainer();

            using (var handler = new HttpClientHandler()
            {
                CookieContainer = cookieContainer
            })
                using (var client = new HttpClient(handler)
                {
                    BaseAddress = baseAddress
                })
                {
                    cookieContainer.Add(baseAddress, new System.Net.Cookie("nagykoru", "igen"));
                    return(await client.GetAsync(uri));
                }
        }
Ejemplo n.º 17
0
		/// <summary>
		/// ログインする
		/// </summary>
		/// <param name="cookieGetter"></param>
		/// <returns></returns>
		public static AccountInfomation Login(ICookieGetter cookieGetter)
		{
			System.Net.Cookie cookie = cookieGetter.GetCookie(new Uri("http://www.nicovideo.jp/"), "user_session");
			if (cookie == null) {
				Logger.Default.LogMessage(string.Format("Login failed, cookie dosen't found"));
				return null;
			}

			System.Net.CookieContainer container = new System.Net.CookieContainer();
			container.Add(cookie);

			AccountInfomation accountInfomation = NicoApiSharp.AccountInfomation.GetMyAccountInfomation(container);
			if (accountInfomation != null) {
				DefaultCookies = container;
				return accountInfomation;
			}

			return null;
		}
Ejemplo n.º 18
0
        private async void button1_Click(object sender, EventArgs e)
        {
            try
            {
                button1.Enabled = false;
                while (true)
                {
                    var browser = ryu_s.BrowserCookie.BrowserManagerMaker.CreateInstance(ryu_s.MyCommon.Browser.BrowserType.Chrome);
                    var cookies = browser.CookieGetter.GetCookieCollection("nicovideo.jp");
                    var cc = new System.Net.CookieContainer();
                    cc.Add(cookies);

                    var settings = new NiconamaBbsGetter.Settings();
                    settings.CacheDir = txt_CacheDir.Text;
                    settings.Cc = cc;
                    settings.CommunityId = txt_communityId.Text;
                    settings.Host = txt_Host.Text;
                    settings.User = txt_User.Text;
                    settings.Pass = txt_Pass.Text;
                    settings.DbName = txt_DbName.Text;

                    var nicoBbs = new NiconamaBbsGetter.BbsGetter(settings);
                    try
                    {
                        await nicoBbs.Do(new Progress<Report>(SetProgress));

                    }
                    catch (System.Net.WebException ex)
                    {
                        ryu_s.MyCommon.Logging.LogException(ryu_s.MyCommon.LogLevel.error, ex);
                        Console.WriteLine(ex.Message);
                    }
                    SetText("30秒待機");
                    await Task.Delay(30 * 1000);
                }
            }
            finally
            {
                button1.Enabled = true;
            }
        }
Ejemplo n.º 19
0
        private async void FrameDataAsync(object s, FrameLoadEndEventArgs e)
        {
            if (e.Url.ToString().Contains("basis/login.do"))
            {
                wb.Stop();

                var result = await Cef.GetGlobalCookieManager().VisitAllCookiesAsync();

                var cookies = new System.Net.CookieContainer();

                result.ToList().ForEach(x => cookies.Add(new System.Net.Cookie(x.Name, x.Value, x.Path, x.Domain)));

                //seta o cookie container;
                WebRequestHelper.Cookies = cookies;

                dynamic httpContent = await WebRequestHelper.PostRequestAsync(
                    "http://gspn6.samsungcsportal.com/basis/common/getChangedTime.jsp",
                    "menuId=MAIN_04&urlType=TOP&varUrl=%2Fbasis%2FmenuServlet.do%3Fmethod%3DgetMenuUrl&varTarget=body");

                if (httpContent == null)
                {
                    return;
                }

                //retorna o id de acesso
                var code = getIDAccessByHtml(httpContent.result);
                if (code == null)
                {
                    Cef.GetGlobalCookieManager().Dispose();
                    Console.WriteLine("Erro de conexão no login. Precione qualquer tecla para acesso offline: ");
                    return;
                }

                tcsDocument.TrySetResult(code);
                wb = null;
                return;
            }
        }
Ejemplo n.º 20
0
        /// <summary>
        /// 通过启动浏览器来开始获取API认证信息
        /// </summary>
        public void startAuth()
        {
            IWebDriver selenium = new ChromeDriver();

            selenium.Navigate().GoToUrl("https://qun.qq.com/member.html");
            while (skey == "")
            {
                foreach (var cookie in selenium.Manage().Cookies.AllCookies)
                {
                    if (cookie.Name.ToLower() == "skey")
                    {
                        skey = cookie.Value;
                    }
                }
                System.Threading.Thread.Sleep(2000);
            }
            foreach (var cookie in selenium.Manage().Cookies.AllCookies)
            {
                cookieContainer.Add(new System.Net.Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain));
            }
            selenium.Close();
            return;
        }
Ejemplo n.º 21
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure <CookiePolicyOptions>(options =>
            {
                options.CheckConsentNeeded    = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddDbContext <ApplicationDbContext>(options =>
                                                         options.UseInMemoryDatabase(nameof(ApplicationDbContext)));

            services.AddDatabaseDeveloperPageExceptionFilter();

            services.AddIdentity <ApplicationUser, IdentityRole>(options => options.SignIn.RequireConfirmedAccount = true)
            .AddEntityFrameworkStores <ApplicationDbContext>()
            .AddDefaultTokenProviders();

            services.Configure <IdentityOptions>(options =>
            {
                options.Password.RequireDigit           = false;
                options.Password.RequireUppercase       = false;
                options.Password.RequireLowercase       = false;
                options.Password.RequireNonAlphanumeric = false;

                options.Lockout.AllowedForNewUsers      = true;
                options.Lockout.MaxFailedAccessAttempts = 10;
                options.Lockout.DefaultLockoutTimeSpan  = TimeSpan.FromMinutes(1);

                options.User.RequireUniqueEmail = true;
            });

            services
            .AddAuthentication(cfg =>
            {
                cfg.DefaultScheme          = IdentityConstants.ApplicationScheme;
                cfg.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer()
            .AddCookie();

            services.Configure <HybridOptions>(Configuration);
            services.AddControllersWithViews();
            services.AddRazorPages();
            services.AddServerSideBlazor();
            services.AddGrpc(options =>
            {
                options.Interceptors.Add <GrpcExceptionInterceptor>();
                options.ResponseCompressionAlgorithm = "gzip";
                options.ResponseCompressionLevel     = System.IO.Compression.CompressionLevel.Optimal;
                options.EnableDetailedErrors         = true;
            });
            services.AddResponseCompression(opts =>
            {
                opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
                    new[] { "application/octet-stream" });
            });
            services.AddCodeFirstGrpc(config =>
            {
                config.EnableDetailedErrors     = true;
                config.ResponseCompressionLevel = System.IO.Compression.CompressionLevel.Optimal;
            });

            services.AddHttpContextAccessor();

            services.AddTransient(sp =>
            {
                var env = sp.GetService <IWebHostEnvironment>();
                var httpContextAccessor = sp.GetService <IHttpContextAccessor>();
                var httpContext         = httpContextAccessor.HttpContext;

                var cookies         = httpContext.Request.Cookies;
                var cookieContainer = new System.Net.CookieContainer();
                foreach (var c in cookies)
                {
                    cookieContainer.Add(new System.Net.Cookie(c.Key, c.Value)
                    {
                        Domain = httpContext.Request.Host.Host
                    });
                }

                var handler = new HttpClientHandler {
                    CookieContainer = cookieContainer
                };
                if (env.IsDevelopment())
                {
                    handler.ServerCertificateCustomValidationCallback = (c, v, b, n) => { return(true); };
                }

                return(handler);
            });

            services.AddTransient(sp =>
            {
                var handler = sp.GetService <HttpClientHandler>();

                if (serverAddressesFeature?.Addresses == null ||
                    serverAddressesFeature.Addresses.Count == 0)
                {
                    return(new HttpClient(handler));
                }

                var insideIIS = Environment.GetEnvironmentVariable("APP_POOL_ID") is string;

                var address = serverAddressesFeature.Addresses
                              .FirstOrDefault(a => a.StartsWith($"http{(insideIIS ? "s" : "")}:"))
                              ?? serverAddressesFeature.Addresses.First();

                var uri = new Uri(address);

                return(new HttpClient(handler)
                {
                    BaseAddress = new Uri($"{uri.Scheme}://localhost:{uri.Port}")
                });
            });

            services.AddScoped <IAuthService, ServerAuthService>();
            services.AddSingleton <CounterStateStorageService>();
            Client.Program.ConfigureCommonServices(services);
        }
Ejemplo n.º 22
0
        private static void StartListen(string hubName, string baseUrl, Action callback)
        {
            connection = new HubConnection(baseUrl);

            connection.Credentials = System.Net.CredentialCache.DefaultCredentials;

            var cc = new System.Net.CookieContainer();
            var cookiename = "userinfo";
            var cookievalue =
                @"3kiJ2IdvqPGaSuxB7J8Kk9l7nwFTV0mMB0gqTY5pLKePBukJ/Onh9fBBGHxWwKDxkTZfLsebSK1QgqRNmsYNrjkYriErk7N0ESyOH426P+ddVfNjYLYF39M22WG0nR/X1ZfHB2AphqnEzlPryVoirqOZa9c5ti7FC55S7O21myqOgJZigkozRj6+65SsdbfG";
            cc.Add(new Uri(baseUrl), new System.Net.Cookie(cookiename, cookievalue));

            connection.CookieContainer =
                Domas.DAP.ADF.Cookie.CookieManger.GetUriCookieContainer(new Uri(baseUrl));

            var hubProxy = connection.CreateHubProxy(hubName);

            Action reConnection = () =>
            {
                try
                {
                    lock (connection)
                    {
                        connection.Stop();
                    }
                }
                finally
                {
                    while (true)
                    {
                        Thread.Sleep(1000);
                        if (connection == null || connection.State == ConnectionState.Disconnected)
                        {

                            break;
                        }
                    }
                    StartListen(hubName, baseUrl, callback);
                }
            };
            hubProxy.On("addMessage", () => Task.Factory.StartNew(() =>
            {
                hubProxy.Invoke("ServerCallback").ContinueWith(task =>
                {
                    if (task.IsFaulted)
                    {

                    }
                    else
                    {
                        callback();
                    }
                });
            }));
            hubProxy.On("CloseClient", () =>
            {
                reConnection();
            });
            var sseTransport = new ServerSentEventsTransport();

            connection.Start(sseTransport).ContinueWith(task =>
            {
                if (task.IsFaulted)
                {
                    throw task.Exception;
                }
                else
                {
                    Console.WriteLine("connected.");
                    connection.Reconnected += () =>
                    {
                        reConnection();
                    };
                }
            }).Wait();


            hubProxy.Invoke("SetClientAgent", "Notifier").ContinueWith(task =>
            {
                if (task.IsFaulted)
                {

                }
                else
                {
                    Console.WriteLine("first invoke success.");
                }
            }).Wait();
        }
        /// <summary>
        /// Attempts to download the Uri and (based on it's MimeType) use the DocumentFactory
        /// to get a Document subclass object that is able to parse the downloaded data.
        /// </summary>
        /// <remarks>
        /// http://www.123aspx.com/redir.aspx?res=28320
        /// </remarks>
        protected Document Download(Uri uri)
        {
            bool success = false;

            // Open the requested URL

            System.Net.WebProxy proxyObject = null;
            if (Preferences.UseProxy)
            {   // [v6] stephenlane80 suggested proxy code
                proxyObject             = new System.Net.WebProxy(Preferences.ProxyUrl, true);
                proxyObject.Credentials = System.Net.CredentialCache.DefaultCredentials;
            }
            // [v6] Erick Brown [work] suggested fix for & in querystring
            string unescapedUri = Regex.Replace(uri.AbsoluteUri, @"&amp;amp;", @"&", RegexOptions.IgnoreCase);

            System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(unescapedUri);

            req.AllowAutoRedirect            = true;
            req.MaximumAutomaticRedirections = 3;
            req.UserAgent = Preferences.UserAgent;             //"Mozilla/6.0 (MSIE 6.0; Windows NT 5.1; Searcharoo.NET; robot)";
            req.KeepAlive = true;
            req.Timeout   = Preferences.RequestTimeout * 1000; //prefRequestTimeout
            if (Preferences.UseProxy)
            {
                req.Proxy = proxyObject;                       // [v6] stephenlane80
            }
            // SIMONJONES http://codeproject.com/aspnet/spideroo.asp?msg=1421158#xx1421158xx
            req.CookieContainer = new System.Net.CookieContainer();
            req.CookieContainer.Add(_CookieContainer.GetCookies(uri));

            // Get the stream from the returned web response
            System.Net.HttpWebResponse webresponse = null;
            try
            {
                webresponse = (System.Net.HttpWebResponse)req.GetResponse();
            }
            catch (System.Net.WebException we)
            {   //remote url not found, 404; remote url forbidden, 403
                ProgressEvent(this, new ProgressEventArgs(2, "skipped  " + uri.AbsoluteUri + " response exception:" + we.ToString() + ""));
            }

            Document currentUriDocument = null;

            if (webresponse != null)
            {
                /* SIMONJONES */

                /* **************** this doesn't necessarily work yet...
                 * if (webresponse.ResponseUri != htmldoc.Uri)
                 * {	// we've been redirected,
                 *  if (visited.Contains(webresponse.ResponseUri.ToString().ToLower()))
                 *  {
                 *      return true;
                 *  }
                 *  else
                 *  {
                 *      visited.Add(webresponse.ResponseUri.ToString().ToLower());
                 *  }
                 * }*/

                try
                {
                    webresponse.Cookies = req.CookieContainer.GetCookies(req.RequestUri);
                    // handle cookies (need to do this in case we have any session cookies)
                    foreach (System.Net.Cookie retCookie in webresponse.Cookies)
                    {
                        bool cookieFound = false;
                        foreach (System.Net.Cookie oldCookie in _CookieContainer.GetCookies(uri))
                        {
                            if (retCookie.Name.Equals(oldCookie.Name))
                            {
                                oldCookie.Value = retCookie.Value;
                                cookieFound     = true;
                            }
                        }
                        if (!cookieFound)
                        {
                            _CookieContainer.Add(retCookie);
                        }
                    }
                }
                catch (Exception ex)
                {
                    ProgressEvent(this, new ProgressEventArgs(3, "Cookie processing error : " + ex.Message + ""));
                }
                /* end SIMONJONES */

                currentUriDocument = DocumentFactory.New(uri, webresponse);
                success            = currentUriDocument.GetResponse(webresponse);
                webresponse.Close();
                ProgressEvent(this, new ProgressEventArgs(2, "Trying index mime type: " + currentUriDocument.MimeType + " for " + currentUriDocument.Uri + ""));

                _Visited.Add(currentUriDocument.Uri);   // [v7] [email protected] capture redirected Urls
                                                        // relies on Document 'capturing' the final Uri
                                                        // this.Uri = webresponse.ResponseUri;
            }
            else
            {
                ProgressEvent(this, new ProgressEventArgs(2, "No WebResponse for " + uri + ""));
                success = false;
            }
            return(currentUriDocument);
        }
Ejemplo n.º 24
0
        private static System.Net.CookieContainer FetLifeCookie()
        {
            string[] pairs = cookieString.Split(';');
            var cookieContainer = new System.Net.CookieContainer();
            Uri fl = new Uri("https://fetlife.com");

            foreach (var item in pairs)
            {
                string[] splitItem = item.Split(new char[] { '=' }, 2);
                System.Net.Cookie cookie = new System.Net.Cookie(splitItem[0].Trim(), splitItem[1].Trim(), "/", fl.Host);
                cookieContainer.Add(cookie);
            }
            return cookieContainer;
        }
Ejemplo n.º 25
0
        /// <summary>
        /// Attempts to download the Uri and (based on it's MimeType) use the DocumentFactory
        /// to get a Document subclass object that is able to parse the downloaded data.
        /// </summary>
        /// <remarks>
        /// http://www.123aspx.com/redir.aspx?res=28320
        /// </remarks>
        protected Document Download(Uri uri)
        {
            bool success = false;

            // Open the requested URL
            System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri.AbsoluteUri);
            req.AllowAutoRedirect            = true;
            req.MaximumAutomaticRedirections = 3;
            req.UserAgent = Preferences.UserAgent;             //"Mozilla/6.0 (MSIE 6.0; Windows NT 5.1; Searcharoo.NET)";
            req.KeepAlive = true;
            req.Timeout   = Preferences.RequestTimeout * 1000; //prefRequestTimeout

            // SIMONJONES http://codeproject.com/aspnet/spideroo.asp?msg=1421158#xx1421158xx
            req.CookieContainer = new System.Net.CookieContainer();
            req.CookieContainer.Add(_CookieContainer.GetCookies(uri));

            // Get the stream from the returned web response
            System.Net.HttpWebResponse webresponse = null;
            try
            {
                webresponse = (System.Net.HttpWebResponse)req.GetResponse();
            }
            catch (System.Net.WebException we)
            {   //remote url not found, 404; remote url forbidden, 403
                ProgressEvent(this, new ProgressEventArgs(2, "skipped  " + uri.AbsoluteUri + " response exception:" + we.ToString() + ""));
            }

            Document htmldoc = null;

            if (webresponse != null)
            {
                /* SIMONJONES */

                /* **************** this doesn't necessarily work yet...
                 * if (webresponse.ResponseUri != htmldoc.Uri)
                 * {	// we've been redirected,
                 *  if (visited.Contains(webresponse.ResponseUri.ToString().ToLower()))
                 *  {
                 *      return true;
                 *  }
                 *  else
                 *  {
                 *      visited.Add(webresponse.ResponseUri.ToString().ToLower());
                 *  }
                 * }*/

                try
                {
                    webresponse.Cookies = req.CookieContainer.GetCookies(req.RequestUri);
                    // handle cookies (need to do this incase we have any session cookies)
                    foreach (System.Net.Cookie retCookie in webresponse.Cookies)
                    {
                        bool cookieFound = false;
                        foreach (System.Net.Cookie oldCookie in _CookieContainer.GetCookies(uri))
                        {
                            if (retCookie.Name.Equals(oldCookie.Name))
                            {
                                oldCookie.Value = retCookie.Value;
                                cookieFound     = true;
                            }
                        }
                        if (!cookieFound)
                        {
                            _CookieContainer.Add(retCookie);
                        }
                    }
                }
                catch (Exception ex)
                {
                    ProgressEvent(this, new ProgressEventArgs(3, "Cookie processing error : " + ex.Message + ""));
                }
                /* end SIMONJONES */

                htmldoc = DocumentFactory.New(uri, webresponse);
                success = htmldoc.GetResponse(webresponse);
                webresponse.Close();
                ProgressEvent(this, new ProgressEventArgs(2, "Trying index mime type: " + htmldoc.MimeType + " for " + htmldoc.Uri + ""));
            }
            else
            {
                ProgressEvent(this, new ProgressEventArgs(2, "No WebResponse for " + uri + ""));
                success = false;
            }
            return(htmldoc);
        }
Ejemplo n.º 26
0
        private System.Net.CookieContainer ParseCookieSettings(string line)
        {
            System.Net.CookieContainer container = new System.Net.CookieContainer();

            // �N�b�L�[�����̑O�ɂ‚��Ă����悭�킩���Ȃ��w�b�_�[���������菜��
            // �ΏہF
            // �@\\x�ƂQ���̂P�U�i���l
            // �@\\\\
            // �@\���Ȃ��ꍇ�̐擪�P����
            string matchPattern = "^(\\\\x[0-9a-fA-F]{2})|^(\\\\\\\\)|^(.)|[\"()]";
            System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(matchPattern, System.Text.RegularExpressions.RegexOptions.Compiled);

            string[] blocks = line.Split(new string[] { "\\0\\0\\0" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string block in blocks) {
                if (block.Contains("=") && block.Contains("domain")) {
                    string header = reg.Replace(block, "");
                    System.Net.Cookie cookie = ParseCookie(header);
                    if (cookie != null) {
                        try {
                            container.Add(cookie);
                        } catch(Exception ex) {
                            CookieGetter.Exceptions.Enqueue(ex);
                        }
                    }
                }
            }

            return container;
        }
Ejemplo n.º 27
0
        private System.Net.CookieContainer ExtractCookies(System.Net.WebResponse response,
                                                          System.Collections.Generic.List <string> list)
        {
            var cookieContainer = new System.Net.CookieContainer();
            // List<string> list = new List<string>() { "expires", "path", "domain", "max-age" };

            var setCookie = response.Headers ["Set-Cookie"].Split(';').ToList();

            for (int i = 0; i < setCookie.Count; i++)
            {
                setCookie [i] = setCookie [i].Trim().ToLower().Replace("httponly,", string.Empty);
                var x = setCookie [i].Count(c => c == ',') > 1;
                if ((!setCookie[i].Contains("expires") && setCookie [i].Contains(",")) ||
                    (setCookie[i].Contains("expires") && setCookie [i].Count(c => c == ',') > 1))
                {
                    var temp = setCookie [i].Split(',');
                    setCookie [i] = temp [0];
                    if (!temp [0].Contains("expires"))
                    {
                        setCookie.Insert(i + 1, temp [1]);
                    }
                    else
                    {
                        setCookie.Insert(i + 1, temp [2]);
                    }
                }
            }

            System.Net.Cookie tempCookie = new System.Net.Cookie();
            for (int i = 0; i < setCookie.Count; i++)
            {
                if (!setCookie[i].Contains("="))
                {
                    continue;
                }

                var temp = setCookie[i].Split('=');
                if (list.TrueForAll(k => !temp[0].Contains(k)))
                {
                    if (tempCookie.Name != string.Empty)
                    {
                        cookieContainer.Add(tempCookie);
                    }

                    tempCookie       = new System.Net.Cookie();
                    tempCookie.Name  = temp[0];
                    tempCookie.Value = temp[1];
                }

                if (temp[0] == "path")
                {
                    tempCookie.Path = temp[1];
                }
                else if (temp[0] == "domain")
                {
                    tempCookie.Domain = (temp[1].First() == '.') ? temp[1].Substring(1) : temp[1];
                }

                if (i == setCookie.Count - 1 && tempCookie.Name != string.Empty)
                {
                    cookieContainer.Add(tempCookie);
                }
            }
            return(cookieContainer);
        }