使用SSL / TSL从网址获取图片

时间:2017-02-08 20:00:22

标签: c# ssl webclient

我的应用程序自动填写近30个字段,只留下少量用户和验证码填写。

我需要从网址获取并显示验证码。

我尝试了不同的方式,但没有一种方法可以正常工作。

网址为captcha link

使用chrome你可以直接获得图像,使用IE你会得到一个文件下载。

首次尝试

使用WebBrowser控件打开网址 如果您尝试使用WebBrowser打开URL,您将开始下载文件 如果您像cap.gif或JPG一样保存它,您将获得正确的图像。

此时我正在尝试自动下载任务以避免为用户显示下载对话框。

像其他SO回答Download file and automatically save it to folder一样,我尝试处理WebBrowser导航

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
    e.Cancel = true;
    WebClient client = new WebClient();

    client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);
    client.DownloadDataAsync(e.Url);
}

或直接使用WebClient

WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadFileAsync(new Uri(url), filepath);

在结果的回调中,您将获得图像文件为空(0字节);

如果你看一下AsyncCompletedEventArgs e,就会产生关于SSL / TSL的错误。

The request was aborted: Could not create SSL/TLS secure channel.   
in System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
in System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result)
in System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)

第二次尝试

将SSL / TSL堆栈包含在WebClient中作为SO回答The request was aborted: Could not create SSL/TLS secure channel

ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(AllwaysGoodCertificate);
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadFileAsync(new Uri(captchaUrl), filepath);

这将在回调中返回相同的错误

第三次尝试 从WebBrowser控件获取图像并将标签转换为PictureBox

如果我尝试在主页面内容表单上提取图像,则可以让我获取图像 按照SO回答How to copy the loaded image in webbrowser to picturebox

[DllImport("user32.dll")]
public static extern bool PrintWindow(IntPtr hwnd, IntPtr hdcBlt, uint nFlags);

public Bitmap CaptureWindow(Control ctl)
{
    //Bitmap bmp = new Bitmap(ctl.Width, ctl.Height);  // includes borders
    Bitmap bmp = new Bitmap(ctl.ClientRectangle.Width, ctl.ClientRectangle.Height);  // content only
    using (Graphics graphics = Graphics.FromImage(bmp))
    {
        IntPtr hDC = graphics.GetHdc();
        try { PrintWindow(ctl.Handle, hDC, (uint)0); }
        finally { graphics.ReleaseHdc(hDC); }
    }
    return bmp;
}

//Methods to get Co-ordinates Of an Element in your webbrowser
public int getXoffset(HtmlElement el)
{
    int xPos = el.OffsetRectangle.Left;
    HtmlElement tempEl = el.OffsetParent;
    while (tempEl != null)
    {
        xPos += tempEl.OffsetRectangle.Left;
        tempEl = tempEl.OffsetParent;
    }
    return xPos;
}

public int getYoffset(HtmlElement el)
{
    int yPos = el.OffsetRectangle.Top;
    HtmlElement tempEl = el.OffsetParent;
    while (tempEl != null)
    {
        yPos += tempEl.OffsetRectangle.Top;
        tempEl = tempEl.OffsetParent;
    }
    return yPos;
}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    // get captcha
    HtmlElement el = webBrowser1.Document.GetElementById("imgCaptcha");
    IHTMLImgElement img = (IHTMLImgElement)el.DomElement;
    Bitmap bmp = new Bitmap(img.width, img.height);

    int CaptchaWidth = getXoffset(el);
    int CaptchaHeight = getYoffset(el);
    Rectangle rect = new Rectangle(CaptchaWidth, CaptchaHeight, img.width, img.height);

    // with this image il always blank
    //webBrowser1.DrawToBitmap(bmp, rect);

    Bitmap bitmap = CaptureWindow(webBrowser1);
    Bitmap croppedImage = bitmap.Clone(rect, System.Drawing.Imaging.PixelFormat.Undefined);
    pictureBox1.BackgroundImage = croppedImage;
}

这项工作很好,但不幸的是,这只有在WebBrowser控件可见时才有效... :(

任何建议都会受到赞赏。

2 个答案:

答案 0 :(得分:1)

经过一些努力,我发现了SSL上的一个问题,服务器进程协议TSL 1.2而不是SSL3

只需替换

//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

现在WebClient运行良好,但在给自己一个正确答案之前,我等待其他人有其他实现。

答案 1 :(得分:1)

我要做的改变是使用async / await所以我不必担心设置WebClient的DownloadFileCompleted

这部分和你一样,

 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

使用异步方法

public async Task DownloadIt(string url ,string filename)
{
     using (var client = new WebClient())
     {
         await client.DownloadFileTaskAsync(new Uri(url), filename);
     }
}

无论何时需要,都可以这样称呼它。

await DownloadIt("https://www.com", @"C:\Path\To\Save");
相关问题