0

网页表格:

<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/子页面。单击“显示”按钮时,它应该填充txtBxSource元素源代码,但事实并非如此。相反,它显示了http://example.com/home/源代码。如果我在一切正常的情况下移动整个代码public void btnLogin_Click,否则它不会。

  1. 我的问题的解决方案是什么?

  2. 问题:一旦我把整个东西放在一个网站/网络主机上并且有人(通过这个网页)在http://example.com/ 网站上登录并使用这个迷你应用程序,http: //example.com/网站会收到自己的IP还是webhost的IP?

4

1 回答 1

0

您遇到的问题是,当页面回发时,CookieContainer 的信息是明确的。你可以通过两种方式解决这个问题,创建一个不会影响回发的 CookieContainer 的视图状态,或者你可以创建一个包含多个子脚本的脚本。您可以为第二个按钮的脚本创建一个单独的公共空白,并像序列一样在第一个按钮中调用此脚本。

于 2012-05-10T14:11:29.790 回答