WebBrowser.Navigate在HTTP上工作但在HTTPS上的相同URL失败(C#)

时间:2018-03-23 14:51:05

标签: c# ssl https webbrowser-control

我的网站有测试环境和生产环境,我不确定这是否只是我的证书问题。我的测试环境有一个真实的证书,我使用digicert生成,但我确实在浏览器信息中看到它说'#34;您与此站点的连接不完全安全。"

我使用WebBrowser在我的网站上生成页面并将其转换为可下载的图像,这是基本代码:

using (var webBrowser = new WebBrowser {ScrollBarsEnabled = false, ScriptErrorsSuppressed = true}){
    webBrowser.Navigate(_url);
    while (webBrowser.ReadyState != WebBrowserReadyState.Complete){
        System.Windows.Forms.Application.DoEvents();
        Thread.Sleep(5);
    }

    while (!(bool)webBrowser.Document.InvokeScript("getDataIsReady"))
    {
        // Exit if requested
        if (ct.IsCancellationRequested)
        {
            ct.ThrowIfCancellationRequested();
        }
        System.Windows.Forms.Application.DoEvents();
        Thread.Sleep(15);
    }

    // Slight pause because sometimes there is a few milliseconds before the GMAP tiles are completely loaded after the "tilesloaded" event fires
    Thread.Sleep(200);

    // Set the size of the WebBrowser control
    webBrowser.Width = _width < 1 ? webBrowser.Document.Body.ScrollRectangle.Width : _width;
    webBrowser.Height = _height < 1 ? webBrowser.Document.Body.ScrollRectangle.Height : _height;

    // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
    bitmap = new Bitmap(webBrowser.Width, webBrowser.Height);
    webBrowser.DrawToBitmap(bitmap, new Rectangle(0, 0, webBrowser.Width, webBrowser.Height));

当我在http上运行时,文档加载,但是在https上,当我检查webBrowser.Document时,我看到标题是&#34;导航已取消&#34;。

这是我的证书还是WebBrowser方法的问题?我希望当我把它推给拥有真正SSL证书的Prod时,它会起作用,但我不想部署它,然后发现它也不能在那里工作。

1 个答案:

答案 0 :(得分:0)

这可能是由于您的ssl证书不受信任。如果证书无效,请在Internet Explorer中加载URL,将其添加为可信证书。然后尝试使用Web浏览器控制。