C#: Need help with httpwebrequest and Twitter

Status
Not open for further replies.

kytro360

Member
Original poster
member
Joined
Sep 9, 2009
Posts
376
Reaction score
6
Points
75
Okay I saw some tutorials, and I got some code. Basically I was doing a test and am trying to log into twitter.com. This is the code I am working with:

Code:
private void btnStartAutomatic_Click(object sender, EventArgs e)
        {
            CookieContainer logincookie;

    string postData = "session%5Busername_or_email%5D="+ textBox3.Text +"&session%5Bpassword%5D="+ textBox4.Text +"&remember_me=1&redirect_after_login=";
    CookieContainer tempCookies = new CookieContainer();
    UTF8Encoding encoding = new UTF8Encoding();
    byte[] byteData = encoding.GetBytes(postData);

    HttpWebRequest postReq = (HttpWebRequest)WebRequest.Create("https://twitter.com/sessions?phx=1");
    postReq.Method = "POST";
    postReq.KeepAlive = true;
    postReq.CookieContainer = tempCookies;
    postReq.ContentType = "application/x-www-form-urlencoded";
    postReq.Referer = "Referer:https://twitter.com/sessions?phx=1";
    postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)";
    postReq.ContentLength = byteData.Length;

    Stream postreqstream = postReq.GetRequestStream();
    postreqstream.Write(byteData, 0, byteData.Length);
    postreqstream.Close();
    HttpWebResponse postresponse = default(HttpWebResponse);

    postresponse = (HttpWebResponse)postReq.GetResponse();
    tempCookies.Add(postresponse.Cookies);
    logincookie = tempCookies;
    StreamReader postreqreader = new StreamReader(postresponse.GetResponseStream());
    StreamReader reader = new StreamReader(postresponse.GetResponseStream());
    string theusercp = reader.ReadToEnd ();

    if (theusercp.Contains("What’s happening?") == true)
    {
        MessageBox.Show("Success");


    }
      
        }
But when I run it theres an error at this line:

Code:
postresponse = (HttpWebResponse)postReq.GetResponse();

I am not sure if the errors popping up because I got the referrer wrong or something.

Can you help?
 

kytro360

Member
Original poster
member
Joined
Sep 9, 2009
Posts
376
Reaction score
6
Points
75
What do you mean by adjusting it to HTTPS? I did a quick google search for "https webrequest" and nothing came up.

By adjusting do you mean change the links to say the prefix https not http?

Checked and all my links are already in HTTPS format. Anything else?

I added this to my code: System.Net.ServicePointManager.Expect100Continue = false;
because its supposed to make Twitter stop looking for something not found in HTTPS requests. I set up two if and else statements

Code:
if (theusercp.Contains("Tweet") == true)
            {
                MessageBox.Show("Success");


            }
            else
            {

                MessageBox.Show("Failure");
            }

And I keep getting failure.

Okay I fixed the issue. I just changed the webrequest and it worked :)
 

Patel

Veteran
veteran
old school
senior
member
Joined
Aug 12, 2010
Posts
1,593
Reaction score
489
Points
133
Location
Inside Tang
Website
itspatel.com
:|

http://twitter.com/ != https://twitter.com

see the s?
 

kytro360

Member
Original poster
member
Joined
Sep 9, 2009
Posts
376
Reaction score
6
Points
75
Yes sir, I changed it to just twitter.com
 
Status
Not open for further replies.