2012-05-02 61 views
0

Web表單:CookieContainer數據丟失了,爲什麼? [C#,Web表單]

<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> 

後面的代碼:

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; 
    } 

您必須先登錄才能看到http://example.com/elements/13579642/子頁面。當點擊Display按鈕時,它應該使用元素源代碼填充txtBxSource,但它不會。相反,它顯示了http://example.com/home/源代碼。如果我移動整個代碼public void btnLogin_Click一切正常,否則它不。

  1. 對我的問題有什麼解決辦法?

  2. 問題:一旦我得到它,如果我把整個事情的網站/網頁主機上運行,​​並有人登錄(通過此網頁)在http://example.com/網站並使用這個迷你應用程序,http://example.com/網站將收到他自己的IP或網絡主機IP?

回答

0

您遇到的問題是,當頁面回發時,CookieContainer的信息已清除。喲可以通過兩種方式解決這個問題,創建CookieContainer的視圖狀態,這將不會影響回發時間,或者您可以使用多個子腳本創建腳本。您可以爲secound按鈕的腳本創建一個separte public void,並在第一個按鈕中調用該腳本,如序列。

相關問題