Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7012395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:15:01+00:00 2026-05-27T22:15:01+00:00

private void LoginButton_Click(object sender, EventArgs e) { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(loginUrl); IAsyncResult

  • 0
private void LoginButton_Click(object sender, EventArgs e)
{
    try
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(loginUrl);
        IAsyncResult result = request.BeginGetResponse(
            new AsyncCallback(DeleResponse), request);
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

And here is the method which called to on button click event

private void DeleResponse(IAsyncResult result)
{
    byte[] PostData = Encoding.UTF8.GetBytes("username=" + userInp.Text + "&password=" + passInp.Text + extraLoginPostString);
    LoginButton.Text = "Logging in...";
    LoginButton.Enabled = false;
    HttpWebRequest request = (HttpWebRequest)result.AsyncState;
    request.Method = "Post";
    request.CookieContainer = authCookie;
    request.ContentType = "application/x-www-form-urlencoded";
    request.AllowAutoRedirect = false;
    postWriter = request.GetRequestStream();
    postWriter.Write(PostData, 0, PostData.Length);
    postWriter.Close();
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
        string serverData = new StreamReader(response.GetResponseStream()).ReadToEnd();
    string loginValidateString = response.GetResponseHeader(loginValidateStringHolder);
    if (loginValidateString.Contains(LoggedKeyword))
    { 
            some process here:
        }
        else if( FAILKEYWORDCHECK HERE)
        {
            login page process here;
        }
    }

The problem is when I check this with fiddler I can see only following header properties.

Connection: Keep-Alive;
Host: www.example.com

What would be the reason that I can’t set properties in the request header?

Edit: Added synchronous request method which I already achieved without any errors.

 private void LoginButton_Click(object sender, EventArgs e)
    {
        try
        {
           LoginButton.Text = "Logging in...";
            LoginButton.Enabled = false;
            byte[] PostData = Encoding.UTF8.GetBytes("username=" + userInp.Text + "&password=" + passInp.Text + extraLoginPostString);
            request = (HttpWebRequest)WebRequest.Create(loginUrl);
            request.Method = "Post";
            request.CookieContainer = authCookie;
            request.ContentType = "application/x-www-form-urlencoded";
            request.AllowAutoRedirect = false;

            postWriter = request.GetRequestStream();
            postWriter.Write(PostData, 0, PostData.Length);
            postWriter.Close();

            response = (HttpWebResponse)request.GetResponse();

            string serverData = new StreamReader(response.GetResponseStream()).ReadToEnd();
            string loginValidateString = response.GetResponseHeader(loginValidateStringHolder);
            if (loginValidateString.Contains(LoggedKeyword))
            {
                MessageBox.Show("Logged in Successfully");

                foreach (Cookie cookieReader in response.Cookies)
                {
                    authCookie.Add(cookieReader);
                }
                Success method continues..
            }
            else if (loginValidateString.Contains(failedLogKeyword))
            {
                Failed process
            }
        }
        catch
        {
            Catchblock
        }
        }

Means, I just know how to set properties for normal requests.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:15:02+00:00Added an answer on May 27, 2026 at 10:15 pm

    You’re trying to set properties of the request when the response is available. You need to set the request properties before you make the request to the server – so you should be setting them in LoginButton_Click, not in the response handling code. Likewise you can’t use GetRequestStream in a callback for BeginGetResponse. Roughly speaking, you want:

    • In the initial event handler:
      • Create the request
      • Set simple properties
      • Call BeginGetRequestStream
    • In the callback handler for BeginGetRequestStream
      • Write out the body data
      • Call BeginGetResponse
    • In the callback handler for BeginGetResponse
      • Handle the response data

    Alternatively, unless you have to use the asynchronous calls, you could just create a separate thread and use the synchronous versions instead. Until the language support in C# 5, that would be simpler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

private void btnMiles_Click(object sender, EventArgs e) { try { int milesless200 = int.Parse(txtMiles.Text); int
. private void Form1_Load(object sender, EventArgs e) { List<CaclulatedData> tests = new List<CaclulatedData> {
Code example: private void comboBox_SelectedIndexChanged(object sender, EventArgs e) { if(some condition) { comboBox.Text =
private void displayResultsButton_Click(object sender, EventArgs e) { gameResultsListView.Items.Clear(); //foreach (Game game in footballLeagueDatabase.games) //{
private void button5_Click(object sender, EventArgs e) { if (domainUpDown2.Text == Battlefield: Bad Company 2)
private void launchbutton_Click(object sender, EventArgs e) { launchbutton.Enabled = false; Process proc = new
private void screensaverWindow_Load(object sender, System.EventArgs e) { this.BringToFront(); this.Focus(); this.KeyPreview = true; this.KeyDown +=
private void btnMail_Click(object sender, EventArgs e) { new formCustomerReportMailer().Show(); } Is it ill-advised to
private void button1_Click(object sender, EventArgs e) { string name; name = textBox5.Text; SqlConnection con10
private void Form1_MouseEnter(object sender, EventArgs e) { this.Opacity = 1.0; }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.