打开新的线程webbrowser

时间:2014-06-23 13:31:33

标签: c# multithreading url webbrowser-control

我正在尝试从default.aspx打开一个新线程,但是我收到错误:     ActiveX控件'8856f961-340a-11d0-a96b-00c04fd705a2'无法实例化,因为当前线程不在单线程单元中。

使用此代码获取此信息:

WebBrowser wb = new WebBrowser();
protected void Page_Load(object sender, EventArgs e)
    {
string urlAddress = "https://www.facebook.com/login.php?login_attempt=1";

        string email = "";
        string pw = "";
        string PostData = String.Format("email={0}&pass={1}", email, pw);

        CookieContainer cookieContainer = new CookieContainer();

        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(urlAddress);
        req.CookieContainer = cookieContainer;
        req.Method = "POST";
        req.ContentLength = PostData.Length;
        req.ContentType = "application/x-www-form-urlencoded";
        req.AllowAutoRedirect = true;
        req.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";

        ASCIIEncoding encoding = new ASCIIEncoding();
        byte[] loginDataBytes = encoding.GetBytes(PostData);
        req.ContentLength = loginDataBytes.Length;
        Stream stream = req.GetRequestStream();
        stream.Write(loginDataBytes, 0, loginDataBytes.Length);

        HttpWebResponse webResp = (HttpWebResponse)req.GetResponse();

        Stream datastream = webResp.GetResponseStream();
        StreamReader reader = new StreamReader(datastream);
        string data = reader.ReadToEnd();

        wb.DocumentText = reader.ReadToEnd();
}

调用第一行时已经出现的错误。

通过对互联网的一些研究,我发现了十几个主题,但是到目前为止我还不能把它放在一个单独的线程中,所以我可以打开浏览器并从这个回复中获得回复。

有人有什么好的建议吗?

0 个答案:

没有答案