0

Web form:

<form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtBxEmail" runat="server"></asp:TextBox>
        <asp:TextBox ID="txtBxPassword" runat="server"></asp:TextBox>
        <asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" />
        <br /><br />
        <asp:TextBox ID="txtBxLink" runat="server" Text="http://example.com/elements/13579642/"></asp:TextBox>
        <asp:Button ID="btnDisplay" runat="server" Text="Display" OnClick="btnDisplay_Click" />
        <br /><br />
        <asp:TextBox ID="txtBxSource" runat="server" TextMode="MultiLine" Width="600" Height="600"></asp:TextBox>
        </div>
    </form>

Code behind:

protected void Page_Load(object sender, EventArgs e)
    {

    }

    CookieContainer loginCookie;

    public void btnLogin_Click(object sender, EventArgs e)
    {
        string postData = "type=0&user=" + txtBxEmail.Text + "&password=" + txtBxPassword.Text + "&remember=0";
        CookieContainer tempCookies = new CookieContainer();
        UTF8Encoding encoding = new UTF8Encoding();
        byte[] byteData = encoding.GetBytes(postData);

        HttpWebRequest postReq = (HttpWebRequest)WebRequest.Create("http://example.com/ajax/login.ajax.php");
        postReq.Method = "POST";
        postReq.KeepAlive = true;
        postReq.CookieContainer = tempCookies;
        postReq.ContentType = "application/x-www-form-urlencoded";
        postReq.Referer = "http://example.com/ajax/login.ajax.php";
        postReq.UserAgent = "Opera/9.80 (Windows NT 6.1; U; en) Presto/2.10.229 Version/11.61";
        postReq.ContentLength = byteData.Length;

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

        postresponse = (HttpWebResponse)postReq.GetResponse();
        tempCookies.Add(postresponse.Cookies);
        loginCookie = tempCookies;
        StreamReader postreqreader = new StreamReader(postresponse.GetResponseStream());
    }

    public void btnDisplay_Click(object sender, EventArgs e)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(txtBxLink.Text);
        request.CookieContainer = loginCookie;
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream());
        string linkSourceCode = reader.ReadToEnd();

        txtBxSource.Text = linkSourceCode;
    }

You have to be logged in to see http://example.com/elements/13579642/ sub page. When clicking the Display button it should populate txtBxSource with the elements source code, but it doesn't. Instead it shows the http://example.com/home/ source code. If I move the whole code in public void btnLogin_Click everything works, otherwise it doesn't.

  1. What would be the solution for my problem?

  2. The Question: Once I get it to work if I put the whole thing on a website/web host and someone logs (via this web page) in the http://example.com/ website and uses this mini application, the http://example.com/ website will receive his own IP or the webhost IP?

1 Answer 1

0

The problem that you have is that when the page is in post back the information of the CookieContainer is been clear. Yo can fix this in two ways creating a viewstate of the CookieContainer that will not affect when postback or you can create a script with multiple sub script. You can creat a separte public void for the script of the secound button and call this script in the first button like a Sequence.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.