我正在尝试使用 C# 中的 webrequest 登录 xenForo 论坛,但我似乎无法使其正常工作,因此我们将不胜感激。
我使用 Fiddler 获取登录时发送的 POST 数据,这是我得到的原始 POST 数据......
POST http://www.----------.com/login/login HTTP/1.1
Host: www.----------.com
Connection: keep-alive
Content-Length: 109
Cache-Control: max-age=0
Origin: http://www.----------.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Referer: http://www.----------.com/login/login
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: xf_session=0c4e132b44ce81bdf93e70c57fe17eb6; __cfduid=d43498195638b2afe52ebaa9e1f97b8b31342586809; __cfduid=d43498195638b2afe52ebaa9e1f97b8b31342586809
login=USERNAME®ister=0&password=PASSWORD&remember=1&cookie_check=1&redirect=forum%2F&_xfToken=
所以在那之后我去创建一个网络请求以试图复制它,这就是我目前拥有的......
private void button1_Click(object sender, EventArgs e)
{
try
{
HttpWebRequest http = WebRequest.Create("http://www.----------.com/login/login") as HttpWebRequest;
http.KeepAlive = true;
http.Method = "POST";
http.AllowAutoRedirect = true;
http.ContentType = "application/x-www-form-urlencoded";
string postData="login=" + usernameBox.Text + "®ister=0&password=" + passwordBox.Text + "&remember=1&cookie_check=1&redirect=forum%2F&_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;
MessageBox.Show(Convert.ToString(y), "Response Code Debug");
foreach(Cookie c in httpResponse.Cookies)
{
MessageBox.Show(c.Name + " = " + c.Value, "Cookie Debug");
}
http = WebRequest.Create("http://www.----------.com/forum") as HttpWebRequest;
http.CookieContainer = new CookieContainer();
http.CookieContainer.Add(httpResponse.Cookies);
http.AllowAutoRedirect=false;
HttpWebResponse httpResponse2 = http.GetResponse() as HttpWebResponse;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Catch Debug");
try
{
Clipboard.SetText(ex.Message);
}
catch
{
}
}
忽略其中的一些,我正在使用消息框来尝试更准确地弄清楚请求发生了什么,但不幸的是它并没有太大帮助。这是我第一次使用 webrequests,所以如果这只是某种愚蠢的错误,我深表歉意。
我只需要能够找出登录是否成功。此外,如果您需要使用 xenForo 站点,只需使用http://www.shadygamer.com,这是我正在尝试使用的站点。
正如我之前所说,感谢任何和所有帮助。谢谢你。:)