使用C#登录Xenforo

时间:2014-05-26 17:00:18

标签: c# rss xenforo

我有一些代码可以在使用RSS在Xenforo论坛的某个部分发布新帖子时通知我。 RSS提要受到保护,这意味着我必须登录论坛才能访问它。

尝试使用C#登录后尝试访问时,出现403错误。如果有人知道为什么代码的登录部分不起作用,我将非常感激!

这是完整的代码:

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

        }
    }
}

这是登录位:

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;

1 个答案:

答案 0 :(得分:1)

访问/ login / login /时会发送以下信息。如果您可以发送我在下面显示的所有信息,那么您很可能会登录。我强烈建议您使用refereruser-agent包含行,并且在访问时会显示一个Cookie主页。

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: