简体   繁体   English

NavigateToPage挂在ASP.NET应用程序中

[英]NavigateToPage hangs in ASP.NET application

I'm writing a web scraper using ScrapySharp that needs to use a simulated browser to log in and access data. 我正在使用ScrapySharp编写一个Web爬虫,该爬虫需要使用模拟的浏览器来登录和访问数据。 The method, with the NavigateToPage call, works in a console application but not in my ASP.Net application. 带有NavigateToPage调用的方法在控制台应用程序中有效,但在我的ASP.Net应用程序中无效。 No NavigateToPage calls go through, including attempted navigations to Google, with or without https, with or without the default cookies parser. 无论是否使用https,是否使用默认cookie解析器,都不会进行NavigateToPage调用,包括尝试导航到Google。

 public static ScrapingBrowser GetESEBrowser()
        {
            ScrapingBrowser browser = new ScrapingBrowser();
            browser.UseDefaultCookiesParser = false;
            WebPage ESE = browser.NavigateToPage(new Uri("http://www.ese-co.com/storefrontCommerce/login.do"));

            PageWebForm login_form = ESE.FindForm("loginForm");
            login_form["usr_name"] = "blahblah";
            login_form["usr_password"] = "blahblah";

            login_form.Submit();

            return browser;
        }

There seems to be a deadlock in HttpRequest.GetResponseAsync, called in ScrapingBrowser.cs:272. 似乎在HttpRequest.GetResponseAsync中出现了死锁,在ScrapingBrowser.cs:272中被调用。 It seems to be related to the problem as described here: https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html 它似乎与此处描述的问题有关: https : //blog.stephencleary.com/2012/07/dont-block-on-async-code.html

Try an async Task like this to replace your GetESEBrowser(): 尝试这样的异步任务来替换您的GetESEBrowser():

public static async Task<ScrapingBrowser> GetESEBrowser()
        {
            ScrapingBrowser browser = new ScrapingBrowser();
            WebPage ESE = await browser.NavigateToPageAsync(new Uri("https://www.test.com"));

            //...

            return browser;
        }

You can call the Task with 您可以通过以下方式调用任务

GetESEBrowser().ConfigureAwait(false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM