如何检测新启动的浏览器进程中页面加载是否失败?
我使用 Process.Start("firefox.exe", "http://localhost/page.aspx"); 我怎么知道页面是否失败? 或者 如何通过HttpWebRequest、HttpWebResponse知道页面是否失败?
当我使用时
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("somepage.aspx");
HttpWebResponse loWebResponse = (HttpWebResponse)myReq.GetResponse();
Console.Write("{0},{1}",loWebResponse.StatusCode, loWebResponse.StatusDescription);
如何返回错误详细信息?
不需要额外的插件和框架。我只想通过 .net 选择这个问题
请有任何想法
I use Process.Start("firefox.exe", "http://localhost/page.aspx");
And how i can know page fails or no?
OR
How to know via HttpWebRequest, HttpWebResponse page fails or not?
When i use
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("somepage.aspx");
HttpWebResponse loWebResponse = (HttpWebResponse)myReq.GetResponse();
Console.Write("{0},{1}",loWebResponse.StatusCode, loWebResponse.StatusDescription);
how can I return error details?
Not need additional plugins and frameworks. I want to choose this problem only by .net
Any Idea please
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 Watin 来自动化 Firefox,而不是 Process.Start。它是一个浏览器自动化框架,可让您正确监控正在发生的情况。
http://watin.sourceforge.net/
编辑:另请参阅 Google Webdriver http://google-opensource.blogspot.com/2009/05/introducing-webdriver.html
Use Watin to automate firefox instead of Process.Start. Its a browser automation framework that will let you monitor what is happening properly.
http://watin.sourceforge.net/
edit: see also Google Webdriver http://google-opensource.blogspot.com/2009/05/introducing-webdriver.html
如果您要生成子进程,则非常困难,并且您可能需要使用每个浏览器的特定 API(例如,FF 和 IE 之间的 API 不一样)。
在许多情况下,exe 会检测现有实例并将请求转发到那里(因此您不能信任退出代码,因为页面甚至还没有在正确的 exe 中被请求),这并没有帮助。
就我个人而言,我尽量避免为这种情况假设任何特定的浏览器;只需启动 url:
这将使用用户的默认浏览器。但你必须希望它出现——如果不做大量工作,你就无法(可靠且稳健地)从外部检查它。
另一种选择是自己读取数据 (
WebClient.Download*
) - 但这可能会遇到复杂的 cookie、登录、用户代理感知等问题。If you are spawning a child-process, it is quite hard and you'd probably need to use each browser's specific API (it won't be the same between FF and IE, for example).
It doesn't help that in many cases the exe detects an existing instance and forwards the request there (so you can't trust the exit-code, since the page hasn't even been requested in the right exe yet).
Personally, I try to avoid assuming any particular browser for this scenario; just launch the url:
This will use the user's default browser. You have to hope it appears though - you can't (reliably and robustly) check that externally without lots of work.
One other option is to read the data yourself (
WebClient.Download*
) - but this may have issues with complex cookies, login, user-agent awareness, etc.使用 HttpWebRequest 类或 WebClient 类来检查这一点。如果 URL 不存在,我认为 Process.Start 不会返回任何内容。
Use HttpWebRequest class or WebClient class to check this. I don't think Process.Start will return something if the URL not exists.
不要以这种形式启动页面。相反,创建一个本地
http://localhost:/wrapper.html
加载http://localhost/page.aspx
然后加载http://localhost:/pass.html
或http://localhost:/fail.html
。 localhost:是您的应用程序实现的一个简单的 HTTP 服务器接口。这个想法是,Javascript 为您提供了浏览器内部的 API,这比浏览器外部的 API 更加标准。由于wrapper.html上的Javascript与后续资源来自同一服务器甚至端口,因此这应该满足当前浏览器中的同源策略。
Don't start the page in this form. Instead, create a local
http://localhost:<port>/wrapper.html
which loadshttp://localhost/page.aspx
and then eitherhttp://localhost:<port>/pass.html
orhttp://localhost:<port>/fail.html
. localhost: is a trivial HTTP server interface implemented by your app.The idea is that Javascript gives you an API inside the browser, which is far more standard than the APIs on the outside of browsers. Since the Javascript on wrapper.html comes from the same server and even port as the subsequent resources, this should satisfy the same-origin policies in current browsers.