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 9057135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:22:37+00:00 2026-06-16T14:22:37+00:00

I am trying to log in to a website from my C# web application

  • 0

I am trying to log in to a website from my C# web application to send emails directly from the application instead of making user visit the website personally.

NOTE: The website, I am trying to log into changes the id of the textboxes everytime the page is loaded, EVEN DURING THE SAME SESSION. Therefore I decided to read the page source, extract the id of the textbox and then use that id while posting the message.

public partial class sender : System.Web.UI.Page
{
    string userID, userPwd, recepsID, msgText, loginResponseString, sessionCode, queryCode;
    private HttpWebRequest initRequest, loginRequest, msgRequest;
    private HttpWebResponse initResponse, loginResponse;
    private Object lockObj = new Object();

    protected void Page_Load(object sender, EventArgs e)
    {
        userID = Request.QueryString["userNumber"];
        userPwd = Request.QueryString["userPwd"];
        recepsID = Request.QueryString["receps"];
        msgText = Request.QueryString["msgBody"];
        if (userID != null && userPwd != null && recepsID != null & msgText != null)
            doLoginAndSendMessage(userID, userPwd, recepsID, msgText);
        else
            Response.Write("Some values are missing");
    }

    public void doLoginAndSendMessage(string uid, string pwd, string recepIds, string msg)
    {
        try
        {
            doLogin(uid, pwd, recepIds, msg);
        }
        catch (Exception ex)
        {
            Response.Write("Sending Failed, Please Try Again");
        }
    }

    public void doLogin(string strUserId, string strPassword, string strIds, string strMessage)
    {
        try
        {
            initRequest = (HttpWebRequest)WebRequest.Create("http://www.somewebsite.com/login.aspx");
            initRequest.CookieContainer = new CookieContainer();
            initRequest.Timeout = 60000;
            StreamReader initSr = new StreamReader(initRequest.GetResponse().GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8"));
            initSr.ReadToEnd();
            initSr.Close();

            loginRequest = (HttpWebRequest)WebRequest.Create("http://www.somewebsite.com/login.aspx");
            loginRequest.CookieContainer = new CookieContainer();
            loginRequest.Timeout = 60000;
            StringBuilder loginString = new StringBuilder();
            loginString.Append("LoginUserId=" + strUserId + "&LoginPassword=" + strPassword + "&RememberMe=1&Login=Login");
            byte[] loginData = Encoding.ASCII.GetBytes(loginString.ToString());
            //to get any cookies from the initial response
            initResponse = (HttpWebResponse)initRequest.GetResponse();
            //setting cookies
            loginRequest.CookieContainer.Add(initResponse.Cookies);
            //Adding Headers
            loginRequest.Method = "POST";
            loginRequest.ContentType = "application/x-www-form-urlencoded";
            loginRequest.ContentLength = loginData.Length;
            Stream loginStream = loginRequest.GetRequestStream();
            loginStream.Write(loginData, 0, loginData.Length);
            loginStream.Close();

            //Reading the response
            StreamReader loginSr = new StreamReader(loginRequest.GetResponse().GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8"));
            loginResponseString = loginSr.ReadToEnd();
            loginSr.Close();

            if (loginResponseString.Contains("inbox.aspx"))
            {
                //get session code
                sessionCode = loginResponseString.Substring(125, 5);
                //call the sendmessage method
                sendMessage(strIds, strMessage);

            }
            else
            {
                Response.Write("Login Failed: Check Username and password");
            }
        }
        catch (Exception ex)
        {
            Response.Write("Sending Failed, Please Try Again");
        }
    }

    public void sendMessage(string strIds, string strMsg)
    {
        try
        {
            string[] ids = strIds.Split(',');
            for (int i = 0; i < ids.Length; i++)
            {
                msgRequest = (HttpWebRequest)WebRequest.Create("http://www.somewebsite.com/writenew.aspx?sessionid=" + sessionCode);
                msgRequest.CookieContainer = new CookieContainer();
                msgRequest.Timeout = 1000000;
                msgRequest.ReadWriteTimeout = 1000000;
                msgRequest.SendChunked = true;
                //to get any cookies from the initial response
                loginResponse = (HttpWebResponse)loginRequest.GetResponse();
                //setting cookies
                msgRequest.CookieContainer.Add(loginResponse.Cookies);
                //Adding Headers
                msgRequest.Method = "POST";
                msgRequest.ContentType = "application/x-www-form-urlencoded";

                Stream msgStream = msgRequest.GetRequestStream();

                Stream respStream = msgRequest.GetResponse().GetResponseStream();

                StreamReader codeRead = new StreamReader(respStream, System.Text.Encoding.GetEncoding("utf-8"));
                string temp = codeRead.ReadToEnd();
                codeRead.Close();
                respStream.Close();
                txtResponse.Text = temp;

                try
                {
                    int starInd = temp.IndexOf("UserId_");
                    //int endInd = starInd + 15;
                    string holder = temp.Substring(starInd, 15);
                    int startInd = holder.IndexOf("_") + 1;
                    queryCode = holder.Substring(startInd, 5);
                    txtSubString.Text = queryCode;
                }
                catch (Exception ex)
                {
                    txtSubString.Text = "SOME ERROR";
                }

                lock (lockObj)
                {
                    StringBuilder msgString = new StringBuilder();
                    msgString.Append("sessionid=" + queryCode + "&GlobalKeyId=1&MessageLength=988&ReceiveId_"
                        + queryCode + "=" + ids[i] + "&Message_" + queryCode + "=" + strMsg
                        + "&SendNow_" + queryCode + "=Send Now");
                    byte[] msgData = Encoding.ASCII.GetBytes(msgString.ToString());
                    msgStream.Write(msgData, 0, msgData.Length);
                    msgStream.Close();
                }

                //Reading the response
                StreamReader msgSr = new StreamReader(respStream, System.Text.Encoding.GetEncoding("utf-8"));
                string msgResponseString = msgSr.ReadToEnd();
                msgSr.Close();
                sessionCode = msgResponseString.Substring(123, 5);
            }
            Response.Write("Message Sent Successfully");
        }
        catch (Exception ex)
        {
            Response.Write("Sending Failed, Please Try Again<br/>" + ex.Message);
        }
    }
}

The application stops when it reaches this line

msgStream.Write(msgData, 0, msgData.Length);

Please help me solve the error. Thank You

  • 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-06-16T14:22:39+00:00Added an answer on June 16, 2026 at 2:22 pm

    When you call GetResponse() it will cause the request built so far to be sent, and the client to fetch the response.

    You need to build your complete request before calling GetResponse(), or your request won’t be complete. Getting the request stream and writing POST data after GetResponse() was called will throw this exception to show that continuing building the request after it has already been sent makes no sense.

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

Sidebar

Related Questions

I am trying to make a Java web application where users can log into
I am trying to connection to my website from my application, for which I
I am trying to log into a website, then go to a link and
I am trying to use PHP and cURL to log in to a website
I'm trying to write a Java class to log in to a certain website.
I'm trying to log everything that happens in my application to two logs: one
I'm trying to download a large file from my Yahoo! web site server which
I'm trying to log in to a website and save an HTML page automatically
I am trying to make authorize by using web.config. In my user registration, it
Evening! I'm trying to log in into a website with zombie.js, but I don't

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.