0

I am trying to open a new thread from my default.aspx, however I get the error: ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.

Getting this with this code:

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();
}

The error I get already with calling the first line.

With a little research on internet I found dozen of subjects on this however I am not able to put this in a single thread till now, so I can open a browser and get the response back from this one.

Anybody any good suggestion?

Jaap Terlouw
  • 125
  • 1
  • 15
  • So start your thread with `ApartmentState.STA`, as with the dupe I'm about to close this question for.... – spender Jun 23 '14 at 13:37
  • https://www.google.com/search?q=8856f961-340a-11d0-a96b-00c04fd705a2 would have been an excellent search... – spender Jun 23 '14 at 13:39
  • Found that thread also however as it already says it does not work on 4.5 – Jaap Terlouw Jun 23 '14 at 14:04
  • 4.5 what? I think this is probably the gold you're looking for... http://stackoverflow.com/questions/21680738/how-to-post-messages-to-an-sta-thread-running-a-message-pump/21684059#21684059 – spender Jun 23 '14 at 14:06
  • 4.5 .NET, hmm that looks pretty nice, will check it later. You have any idea how I can let the page load complete with javascript and than get the page? – Jaap Terlouw Jun 23 '14 at 14:10

0 Answers0