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. 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?
Tomo84
Member
30 Points
25 Posts
CookieContainer data gets lost, why? [C#, web form]
Apr 30, 2012 03:34 PM|LINK
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. 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?