如何从WebBrowser控件下载图像? [英] How to Download images from WebBrowser Control?

查看:88
本文介绍了如何从WebBrowser控件下载图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我想保存来自 WebBrowser 的图像直接控制而不是使用 webclient HttpWebRequest 甚至是图片网址...问题是当我尝试使用网址下载图片时,某些图片返回 403错误禁止因为我需要签名进入该网站。所以我需要使用已经签名的 WebBrowser 控件下载,我尝试使用 WebClient HttpWebRequest 通过复制WebBrowser Cookies,登录使用POST登录页面...但所有这些都不起作用,所以我需要直接从 WebBrowser 下载图像而不需要需要在页面加载后再次下载它们。

谢谢!

解决方案

没有从WebBrowser控件下载。如果在 WebBrowser 中呈现某些内容,则已下载。看起来您只需要在本地驱动器上的某些文件中获取图像。如果与浏览器无关,你可能已经了解自己,你真的需要使用 WebClient HttpWebRequest (我会建议第二个),但做得正确。而且,很可能,Web浏览器cookie很可能是无关紧要的。这是逻辑:首先,你没有任何cookie,但你设法下载了所有。您需要通过正确的凭证来注意身份验证,而不是cookie。请参阅:

http:/ /stackoverflow.com/questions/19764113/how-to-use-httpwebrequest-credentials-property-for-basic-authentication [ ^ ],

http://msdn.microsoft.com/en-us/library/system .net.httpwebrequest.credentials%28v = vs.110%29.aspx [ ^ ]。



那说,你刚才需要像浏览器一样在HTTP协议中重现完全相同的客户端操作。但是,如果你混淆了一些你无法识别的东西该怎么办?这通常发生在一些更棘手的情况下。我可以看到两种方法来解决它:

  1. 非常彻底地查看页面源,并找出它究竟是做什么的。您可以使用查看页面源之类的东西在浏览器中执行此操作。

    它可以只是一些锚点,但它也可以是HTML表单,HTTP参数与名称 - 值键一起传递,根据当时 name 表单控件的属性;它可能是AJAX调用;然后你需要下载所有的Javascript代码并查看它的功能。
  2. 或者,您可以使用一些HTTP间谍软件并分析所有通过的HTTP请求。当你可以忽略许多不相关的细节时,这是一种非常简单的方法。例如,我使用HttpFox,一个Mozilla浏览器的插件,但它可以是其他任何东西。这些工具对 Web抓取非常有用。





-SA

Hey, I want to save images from WebBrowser control Directly and not using webclient or HttpWebRequest or even the image URL... the problem is when I try to download an image using its URL some images return 403 error Forbidden because I need to be Signed in to the site. So I need to download using an already signed in WebBrowser control, I've tried to Download image using WebClient and HttpWebRequest by copying WebBrowser Cookies, logining in Using "POST" to login page... But all that didn't work, So I need to Download images directly from WebBrowser without the need to download them again after page load.
Thanks!

解决方案

There is no such thing as "downloading from WebBrowser control". If something is rendered in a WebBrowser, it is already downloaded. It looks like you just need to obtain the images in some files on your local drive. If has nothing to do with a browser, as you probably understand yourself, you really need to use WebClient or HttpWebRequest (I would advise the second one), but do it correctly. Also, most likely, Web browser cookies, are, most likely, irrelevant. Here is the logic: in first place, you did not have any cookies, but yet you managed to download it all. Instead of cookies, you need to pay attention for authentication itself, through proper credentials. Please see:
http://stackoverflow.com/questions/19764113/how-to-use-httpwebrequest-credentials-property-for-basic-authentication[^],
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.credentials%28v=vs.110%29.aspx[^].

That said, you just need to reproduce exact same client-side action in an HTTP protocol as your browser did. But what to do if you are confusing something which you cannot identify? That often happens in some more tricky situations. I can see two ways to address it:

  1. Look at the page source very thoroughly and find out what exactly it does. You can do it in a browser using something like "View Page Source".
    It can be just some anchor, but it can also be an HTML form, with HTTP parameters passed with name-value keys, according to then name attributes of the controls of the form; it could be AJAX calls; and then you would need to download all Javascript code and see what it does.
  2. Alternatively, you can use some HTTP spy software and analyze all HTTP requests coming through. It's a pretty simple way when you can ignore many unrelated detail. I, for example, use "HttpFox", a plug-in to the Mozilla browser, but it can be anything else. Such tools are very powerful for Web scraping.



—SA


这篇关于如何从WebBrowser控件下载图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆