如何在asp.net中使用webBrowser?

时间:2014-06-05 14:10:21

标签: asp.net

我的代码如下:

 protected void Page_Load(object sender, EventArgs e)
    {

        webBrowser1 = new WebBrowser();

        webBrowser1.Navigate("http://www.wine-searcher.com/wine-valuation-f.lml");


        webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);


    }



    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {

        webBrowser1.Document.GetElementById("wn1_input").InnerText = "William Fevre Les Clos, Chablis Grand Cru, France";
        webBrowser1.Document.GetElementById("vt1_input").InnerText = "NV";
        webBrowser1.Document.GetElementById("valButton").InvokeMember("click");
        TextBox1.Text= webBrowser1.Document.GetElementById("value1").InnerHtml;
    }

当我调试时,我得到nullreferenceException关于我的第一行:

webBrowser1.Document.GetElementById("wn1_input").InnerText = "William Fevre Les Clos, Chablis Grand Cru, France";  

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您不应该在.NET上使用WebBrowser控件。如果你想获取一个Html页面,你有很多选择,比如WebClient类:

http://msdn.microsoft.com/en-us/library/fhd1f0sw(v=vs.110).aspx

http://msdn.microsoft.com/en-us/library/ms144202(v=vs.110).aspx

或HttpClient

http://msdn.microsoft.com/it-it/library/system.net.http.httpclient.aspx

要获取html文档的内容,您可以尝试移植HtmlTidy库,例如http://sourceforge.net/projects/tidynet/