簡體   English   中英

HttpWebRequest登錄頁面,但與答案始終相同的html代碼

[英]Httpwebrequest login to page but always same html code as answer

我試圖通過C#在網站上登錄,然后我想在登錄后讀出網站提供的HTML代碼。

問題是我正在發送登錄信息,但是卻收到了與請求網站主頁相同的HTML代碼。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Media;
using System.Threading;


namespace OgameBot
{
    public partial class Form1 : Form
    {
        string responsePath = @"C:\Users\mightymate\Desktop\response.txt";
        CookieContainer OurCookies = new CookieContainer();
        string page;

        public Form1()
        {

            InitializeComponent();
            //string[] currentFile = File.ReadAllLines(responsePath);
            //foreach (string s in currentFile)
            //{
            //    richTextBox1.AppendText(s+"\n");
            //}
            GetPage("http://de.ogame.gameforge.com/main/login", true);
            Thread.Sleep(1000);
            PostPage("http://de.ogame.gameforge.com/main/login", "kid=&uni=s123-de.ogame.gameforge.com&login=Test&pass=1234", true);
            //PostPage("http://de.ogame.gameforge.com/main/login", "kid=&uni=s123-de.ogame.gameforge.com&login=Test&pass=1234", true);

        }


        private void GetPage(string URL, bool write)
        {
            HttpWebRequest WR = (HttpWebRequest)WebRequest.Create(URL);


            WebHeaderCollection WHC = WR.Headers;

            int temp = WHC.Count;

            WR.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0";
            WR.CookieContainer = OurCookies;
            WR.Method = "GET";
            HttpWebResponse HWR = (HttpWebResponse)WR.GetResponse();
            Stream dataStream = HWR.GetResponseStream();

            if(write)
            {


                string response;
                using (StreamReader SR = new StreamReader(dataStream))
                {
                    response = SR.ReadToEnd();
                }
                richTextBox1.AppendText(GetStringFromCookieContainer(OurCookies, URL));
                richTextBox1.AppendText(response);
                page = response;

            }

        }

        private void PostPage(string URL, string Request, bool write)
        {
            byte[] DataToSend = StringToByteArray(Request);
            HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(URL);

            WebReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0";
            WebReq.CookieContainer = OurCookies;
            WebReq.Method = "POST";
            WebReq.ContentLength = DataToSend.Length;
            WebReq.ContentType = "application/x-wwww-form-urlencoded";
            using (Stream SendingStream = WebReq.GetRequestStream())
            {
                SendingStream.Write(DataToSend, 0, DataToSend.Length);
            }

            if(write)
            {
                using (HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse())
                {
                    using (StreamReader SR = new StreamReader(WebResp.GetResponseStream()))
                    {
                        string serverReponse = SR.ReadToEnd();
                        richTextBox2.AppendText(GetStringFromCookieContainer(OurCookies, URL)+"\n\n");

                        richTextBox2.AppendText(serverReponse);

                    }
                }
            }
        }

        private byte[] StringToByteArray(string s)
        {            
            return new ASCIIEncoding().GetBytes(s);
        }

        private string GetStringFromCookieContainer(CookieContainer CC, string fromUrl)
        {
            string cookieString=string.Empty;
            CookieCollection currentCookies = OurCookies.GetCookies(new System.Uri(fromUrl));
            foreach (Cookie C in currentCookies)
            {
                cookieString += "Comment: " + C.Comment + "\n" +
                    "CommentUri: " + C.CommentUri + "\n" +
                    "Domain: " + C.Domain + "\n" +
                    "Expires: " + C.Expires + "\n" +
                    "Name: " + C.Name + "\n" +
                    "Path: " + C.Path + "\n" +
                    "Port: " + C.Port + "\n" +
                    "Secure: " + C.Secure + "\n" +
                    "Timestamp: " + C.TimeStamp + "\n" +
                    "Value: " + C.Value + "\n" +
                    "Version: " + C.Version + "\n";
            }
            return cookieString;
        }

    }
}

您需要保留cookie,以便網站看到您已登錄。

創建一個CookieContainer並在兩個請求上都進行設置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM