Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log in to a vbulletin forum with C#?

I'm trying to log into a vbulletin forum. I got this far:

private string login(string url, string username, string password)
{
string values = "vb_login_username={0}&vb_login_password={1}"
values += "&securitytoken=guest&cookieuser=checked&do=login";

values = string.Format(values, username, password);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
CookieContainer a = new CookieContainer();
req.CookieContainer = a;

System.Net.ServicePointManager.Expect100Continue = false; // prevents 417 error

using (StreamWriter writer = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII)) 
{ writer.Write(values); }

this.response = (HttpWebResponse)req.GetResponse(); 

StringBuilder output = new StringBuilder();

foreach (var cookie in response.Cookies)
{
output.Append(cookie.ToString());
output.Append(";");
}


return output.ToString();
} 

It looks like i am getting logged in, but when i download the page, i can't find my username in it.

Do you guys see anything that i might be doing wrong?

Thanks in advance!

like image 397
Yustme Avatar asked Mar 03 '10 21:03

Yustme


2 Answers

You can do it using HTTP Request

Use this method

public string Login(Uri ActionUrl, string postData)
        {

            gRequest = (HttpWebRequest)WebRequest.Create(formActionUrl);
            gRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.1.8) Gecko/20100202 Firefox/3.5.8 GTBDFff GTB7.0";

            gRequest.CookieContainer = new CookieContainer();
            gRequest.Method = "POST";
            gRequest.Accept = " text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8, */*";
            gRequest.KeepAlive = true;
            gRequest.ContentType = @"application/x-www-form-urlencoded";


            #region CookieManagement
            if (this.gCookies != null && this.gCookies.Count > 0)
            {

                gRequest.CookieContainer.Add(gCookies);
            }


            try
            {

                string postdata = string.Format(postData);
                byte[] postBuffer = System.Text.Encoding.GetEncoding(1252).GetBytes(postData);
                gRequest.ContentLength = postBuffer.Length;
                Stream postDataStream = gRequest.GetRequestStream();
                postDataStream.Write(postBuffer, 0, postBuffer.Length);
                postDataStream.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

            }



            try
            {
                gResponse = (HttpWebResponse)gRequest.GetResponse();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);

            }



            //check if the status code is http 200 or http ok

                if (gResponse.StatusCode == HttpStatusCode.OK)
                {
                    //get all the cookies from the current request and add them to the response object cookies

                    gResponse.Cookies = gRequest.CookieContainer.GetCookies(gRequest.RequestUri);
                    //check if response object has any cookies or not
                    if (gResponse.Cookies.Count > 0)
                    {
                        //check if this is the first request/response, if this is the response of first request gCookies
                        //will be null
                        if (this.gCookies == null)
                        {
                            gCookies = gResponse.Cookies;
                        }
                        else
                        {
                            foreach (Cookie oRespCookie in gResponse.Cookies)
                            {
                                bool bMatch = false;
                                foreach (Cookie oReqCookie in this.gCookies)
                                {
                                    if (oReqCookie.Name == oRespCookie.Name)
                                    {
                                        oReqCookie.Value = oRespCookie.Name;
                                        bMatch = true;
                                        break; // 
                                    }
                                }
                                if (!bMatch)
                                    this.gCookies.Add(oRespCookie);
                            }
                        }
                    }
            #endregion



                    StreamReader reader = new StreamReader(gResponse.GetResponseStream());
                    string responseString = reader.ReadToEnd();
                    reader.Close();
                    //Console.Write("Response String:" + responseString);
                    return responseString;
                }
                else
                {
                    return "Error in posting data";
                } 

        }
like image 162
Shrikant Soni Avatar answered Oct 19 '22 14:10

Shrikant Soni


I'm not sure if I follow you. What exactly do you mean with VBB?

If you mean posting a thread in vb.com, I can tell you that there is a thread opened in vb.org in forum 'vb3 programming discussion' (not posted by me). It should be on the 3rd page, starts with 'C#'.

If you mean something else with your post, can you clarify it a bit?

like image 45
Yustme Avatar answered Oct 19 '22 13:10

Yustme