Login to Xenforo using C#

Tomtiger11

New Member
#1
I have some code that is meant to notify me when a new post has been made in a certain section of a Xenforo forum using RSS. The RSS feed is protected, meaning I have to be logged in to the forum to access it.

When I try to access it after trying to login using C#, I get a 403 error. If anyone knows as to why the login section of code isn't working, I'd be very grateful!

This is the full code:
Mã:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Net;
using System.IO;
using System.Web;
using System.Xml;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            XmlTextReader rssReader;
            XmlDocument rssDoc = null;
            XmlNode nodeRss = null;
            XmlNode nodeChannel = null;
            String title;
            String text;
            HttpWebRequest http = WebRequest.Create("http://www.aerolitegaming.com/login/login/") as HttpWebRequest;
            http.KeepAlive = true;
            http.Method = "POST";
            http.AllowAutoRedirect = true;
            http.ContentType = "application/x-www-form-urlencoded";
            string postData = "login=SNIP&register=0&password=SNIP&remember=1&cookie_check=0&_xfToken=";
            byte[] dataBytes = UTF8Encoding.UTF8.GetBytes(postData);
            http.ContentLength = dataBytes.Length;
            using (Stream postStream = http.GetRequestStream())
            {
                postStream.Write(dataBytes, 0, dataBytes.Length);
            }
            HttpWebResponse httpResponse = http.GetResponse() as HttpWebResponse;
            int y = (int)httpResponse.StatusCode;
            Console.WriteLine(Convert.ToString(y), "Response Code Debug");
            foreach (Cookie c in httpResponse.Cookies)
            {
                Console.WriteLine(c.Name + " = " + c.Value, "Cookie Debug");
            }
            http = WebRequest.Create("http://www.aerolitegaming.com/") as HttpWebRequest;
            http.CookieContainer = new CookieContainer();
            http.CookieContainer.Add(httpResponse.Cookies);
            http.AllowAutoRedirect=false;
            HttpWebResponse httpResponse2 = http.GetResponse() as HttpWebResponse;
            // RSS read
            try
            {
                rssReader = new XmlTextReader("http://aerolitegaming.com/forums/in-game-reports.132/");
                rssDoc = new XmlDocument();
                rssDoc.Load(rssReader);
            }
            catch (WebException e)
            {
                Console.WriteLine("This program is expected to throw WebException on successful run." +
                                    "\n\nException Message :" + e.Message);
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
                    Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            // Loop for the <rss> tag
            for (int i = 0; i < rssDoc.ChildNodes.Count; i++)
            {
                // If it is the rss tag
                if (rssDoc.ChildNodes[i].Name == "rss")
                {
                    // <rss> tag found
                    nodeRss = rssDoc.ChildNodes[i];
                }
            }
            // Loop for the <channel> tag
            for (int i = 0; i < nodeRss.ChildNodes.Count; i++)
            {
                // If it is the channel tag
                if (nodeRss.ChildNodes[i].Name == "channel")
                {
                    // <channel> tag found
                    nodeChannel = nodeRss.ChildNodes[i];
                }
            }
            // Set the labels with information from inside the nodes
            title = "Title: " + nodeChannel["title"].InnerText;
            text = "Description: " + nodeChannel["description"].InnerText;
            Console.WriteLine(title);
            Console.WriteLine(text);

        }
    }
}
And this is the login bit:
Mã:
HttpWebRequest http = WebRequest.Create("http://www.aerolitegaming.com/login/login/") as HttpWebRequest;
http.KeepAlive = true;
http.Method = "POST";
http.AllowAutoRedirect = true;
http.ContentType = "application/x-www-form-urlencoded";
string postData = "login=SNIP&register=0&password=SNIP&remember=1&cookie_check=0&_xfToken=";
byte[] dataBytes = UTF8Encoding.UTF8.GetBytes(postData);
http.ContentLength = dataBytes.Length;
using (Stream postStream = http.GetRequestStream())
{
    postStream.Write(dataBytes, 0, dataBytes.Length);
}
HttpWebResponse httpResponse = http.GetResponse() as HttpWebResponse;
int y = (int)httpResponse.StatusCode;
Console.WriteLine(Convert.ToString(y), "Response Code Debug");
foreach (Cookie c in httpResponse.Cookies)
{
    Console.WriteLine(c.Name + " = " + c.Value, "Cookie Debug");
}
http = WebRequest.Create("http://www.aerolitegaming.com/") as HttpWebRequest;
http.CookieContainer = new CookieContainer();
http.CookieContainer.Add(httpResponse.Cookies);
http.AllowAutoRedirect=false;
HttpWebResponse httpResponse2 = http.GetResponse() as HttpWebResponse;
 

Admin

Administrator
Thành viên BQT
#2
The information below is sent when accessing /login/login/. If you can send all the information that I have shown below then you will most likely log in. I would highly recommend including the lines with referer and user-agent and there is a cookie that you get when accessing the home page.
Mã:
Remote Address:37.187.140.198:80
Request URL:http://aerolitegaming.com/login/login
Request Method:POST
Status Code:303 See Other
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4,it;q=0.2
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:91
Content-Type:application/x-www-form-urlencoded
Cookie:xf_session=89e197b24545180ebedb364f852853c2
Host:aerolitegaming.com
Origin:http://aerolitegaming.com
Referer:http://aerolitegaming.com/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
Form Dataview sourceview URL encoded
login:LG
register:0
password:PSW
cookie_check:1
redirect:/
_xfToken:
 
Top