c# - 使用 SSL/TLS 从 url 获取图像

标签 c# ssl webclient

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

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

我尝试了不同的方式,但没有一个能满足我的需要。

网址是 captcha link

使用 chrome 可以直接获取图片,使用 IE 可以下载文件。

第一次尝试

用WebBrowser控件打开url 如果您尝试使用 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 堆栈作为 SO 答案包含到 WebClient 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控件获取图片并将tag转换为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 控件可见时才有效...... :(

如有任何建议,我们将不胜感激。

最佳答案

经过一些变通后,我发现一个关于 SSL 的问题,服务器进程协议(protocol) TSL 1.2 而不是 SSL3

只是替换

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

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

关于c# - 使用 SSL/TLS 从 url 获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42122525/

相关文章:

Java HTMLUnit WebClient ScriptException 错误

windows - 从 Windows Powershell 脚本上传到 Artifactory

c# - 如何在 C# 中重命名 string[] 的 xml 元素?

azure - 下载 AzureML 模型引发 SSL 错误

C# SSL服务器模式必须使用带有对应私钥的证书

java - 如何配置 JMX over SSL 使用的 TLS 版本?

c# - 十进制三进制不起作用

c# - 如何解决这个错误 : RabbitMQ. Client.Exceptions.BrokerUnreachableException: 'None of the specified endpoints were reachable'

c# - 遍历具有可为空类型的 Enumerable 集合

c# - 在 PowerShell v2 中编译新类型 - Cookie Aware WebClient