0

サイト内の httpWebRequest と Cookie に関するすべての回答を読みましたが、私の問題はまだ解決されていません。Web サイトにログインする (正しくログに記録する) winform アプリケーションがありますが、その Cookie を使用して別のページにログインすることはできません。 PHPSESSID を使用するなど、多くのソリューションを試しました。効果的でした。これが私のコードです:

           HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("(Login page)");

        webRequest.Method = "POST";
        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.KeepAlive = true;

        ASCIIEncoding encoding = new ASCIIEncoding();
        byte[] data = encoding.GetBytes("username=uname&password=pass&submit=Button");

        webRequest.ContentLength = data.Length;
        CookieContainer CookieContainer = new CookieContainer();
        webRequest.CookieContainer = CookieContainer;


        Stream newStream = webRequest.GetRequestStream();
        newStream.Write(data, 0, data.Length);
        newStream.Close();
        HttpWebResponse webResponse;
        webResponse = (HttpWebResponse)webRequest.GetResponse();
        HttpWebRequest webRequest1 = (HttpWebRequest)WebRequest.Create("(My control panel page)");
        webRequest1.Method = "GET";
        webRequest1.KeepAlive = true;
        webRequest1.CookieContainer=new CookieContainer();
        foreach (Cookie cook in webResponse.Cookies)
        {
            webRequest1.CookieContainer.Add(cook);
        }
        webRequest.ContentType = "application/x-www-form-urlencoded";

        webResponse = (HttpWebResponse)webRequest1.GetResponse();



        string html;
        using (Stream strmresponse = webResponse.GetResponseStream())
        {
            using (StreamReader reader = new StreamReader(strmresponse, Encoding.UTF8))
            {
                html = reader.ReadToEnd();
            }
        }
        textBox1.Text = html;
4

1 に答える 1

1

それでも気にするかどうかはわかりませんが、この質問への回答を確認してください。複数のリクエストで Cookie を再利用する方法が示されています。

于 2013-03-26T19:54:33.003 に答える