简体   繁体   中英

Httpwebrequest login to page but always same html code as answer

I´m trying to login via C# on a website and after that I´d like to read out the HTML code that the website offers, after beeing logged in.

The problem is that I´m sending the login informations, but i´m receiving the same HTML code as if I´d request the main page of the website.

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

    }
}

You need to preserve the cookies so that the website sees that you're logged in.

Create a CookieContainer and set it on both requests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM