Skip to main content
 首页 » 编程设计

c#之如何在 HTTPWebRequest 期间阻止 Internet Explorer 安全警报

2024年02月23日30leader

在检索 HTTPWebRequest 时,有没有一种方法可以防止出现错误消息或以编程方式对弹出的消息单击"is"?下面显示的代码会导致一条警告消息,其文本为“此页面存在未指定的潜在安全风险。您要继续吗?”消息的标题是“Internet Explorer”。关于可能导致错误消息的原因的一些线索是:

• 它似乎不是由代码行 ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true) 中的 sslPolicyErrors 引起的;应该防止这种情况。

• 设置了 Internet Explorer 选项,以便网站 http://jobsearch.monster.com在受信任的站点列表中。

• 该消息在执行最后一行代码后出现:-oDoc.close();

• 可以通过运行代码以响应单击窗口窗体上的按钮来观察消息。

• 错误消息必须手动单击三次(是或否)才能使消息消失。

• 这是迄今为止唯一导致此错误消息的网站。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
//using System.Threading; 
using System.Net; 
using mshtml; 
 
namespace FitCShp 
{ 
    public partial class frmThreadIssue : Form 
    { 
        public frmThreadIssue() 
        { 
            InitializeComponent(); 
        } 
 
        private void button2_Click(object sender, EventArgs e) 
        { 
            HTMLDocumentClass oDoc; 
            oDoc = GetDoc(); 
        } 
 
        public HTMLDocumentClass GetDoc() 
        { 
            string WebAddrs = "http://jobsearch.monster.com/PowerSearch.aspx?q=proven%20record%20relationship&where=portland%2C%20or&rad=20&rad_units=miles&tm=60"; 
            IHTMLDocument2 oDoc = null; 
            HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(WebAddrs); //build the request 
            ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);  
            Req.AllowAutoRedirect = true; 
            Req.ContentType = "application/x-www-form-urlencoded"; 
            Req.KeepAlive = false; 
            Req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11'"; 
            HttpWebResponse resp = (HttpWebResponse)Req.GetResponse(); 
            //Get the encoding of the response 
            System.Text.Encoding Enc = System.Text.Encoding.GetEncoding(resp.CharacterSet); 
            StreamReader sr = new StreamReader(resp.GetResponseStream(), Enc);  //Create the stream reader 
            string sDoc = sr.ReadToEnd();      //read the stream that represents the Webdoc 
            sr.Close();                         //close the stream   
            HTMLDocument ProfileHTML = new HTMLDocument(); 
            oDoc = (IHTMLDocument2)ProfileHTML; 
            oDoc.write(sDoc); 
            oDoc.close();       //--- Error occurs here 
            return (HTMLDocumentClass)oDoc; 
        } 
    } 
} 

请您参考如下方法:

如果您的页面在 HTTPS 上下文中运行,但通过 HTTP 引用其他站点的元素,您将收到此安全警告。确保在 URL 中使用 HTTPS 引用所有引用的内容(例如图像、样式表等)。