无法使用 HttpWebRequests 登录

问题描述 投票:0回答:2

我正在尝试使用 httpwerequests 登录论坛,但到目前为止我没有成功,这是我的代码:

string url = "http://www.warriorforum.com/";

var bytes = Encoding.Default.GetBytes(@"vb_login_username=MyUsername&cookieuser=1&vb_login_password=&s=&securitytoken=guest&do=login&vb_login_md5password=d9350bad28eee253951d7c5211e50179&vb_login_md5password_utf=d9350bad28eee253951d7c5211e50179");
var container = new CookieContainer();

var request = (HttpWebRequest)(WebRequest.Create(url));
request.CookieContainer = container;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/535.2";
request.ContentLength = bytes.Length;
request.Method = "POST";
request.KeepAlive = true;
request.AllowAutoRedirect = true;
request.AllowWriteStreamBuffering = true;
request.CookieContainer = container;
using (var requestStream = request.GetRequestStream())
    requestStream.Write(bytes, 0, bytes.Length);

var requestResponse = request.GetResponse();
using (var responsStream = requestResponse.GetResponseStream())
{
    if (responsStream != null)
    {
        using (var responseReader = new StreamReader(responsStream))
        {
            var responseStreamReader = responseReader.ReadToEnd();
            richTextBox1.Text = responseStreamReader; //this is to read the page source after the request
        }
    }
}

请求后,响应只是同一页面,没有任何变化,没有消息告诉我我输入了错误的密码或类似的东西。

c# authentication httpwebrequest httpwebresponse vbulletin
2个回答
0
投票

我刚刚使用我的通用 VBulletin 登录功能进行了测试,它似乎工作正常:

private static bool VBulletinLogin(Uri loginUrl, string user, string password)
{
    var postParams = new[] {
        new HttpParam("vb_login_username", user),
        new HttpParam("cookieuser", "1"),
        new HttpParam("vb_login_password", password),
        new HttpParam("securitytoken", "guest"),
        new HttpParam("do", "login"),
    };

    var http = new HttpContext();
    var src = http.GetEncodedPageData(loginUrl, HttpRequestType.POST, postParams);
    return src.ResponseData.Contains("Thank you for logging in");
}

不幸的是,这使用了我的

HttpContext
类,它是我一直在编写的库的一部分,并且功能相当相互交织。不过,希望它至少能让您了解帖子参数。我还包含了我自己的类中的一些有用的结构/函数,它们应该会有所帮助。 (注意,需要引用 .NET 3.5
System.Web
命名空间。

第一个有用的结构,HttpParam:

public struct HttpParam
{
    private string _key;
    private string _value;

    public string Key { get { return HttpUtilty.UrlEncode(_key); } set { _key = value; } }
    public string Value { get { return HttpUtility.UrlEncode(_value); } set { _value = value; } }

    public HttpParam(string key, string value)
    {
        _key = key;
        _value = value;
    }

    public override string ToString()
    {
        return string.Format("{0}={1}", Key, Value);
    }
};

还有一个与之配套的功能:

private static string GetQueryString(HttpParam[] args)
{
    return args != null
            ? string.Join("&", Array.ConvertAll(args, arg => arg.ToString()))
            : string.Empty;
}

这些的组合将帮助您生成一致、安全的查询字符串。所以在上述情况下:

var postParams = new[] {
    new HttpParam("vb_login_username", user),
    new HttpParam("cookieuser", "1"),
    new HttpParam("vb_login_password", password),
    new HttpParam("securitytoken", "guest"),
    new HttpParam("do", "login"),
};

var queryString = GetQueryString(postParams);

会给你一些类似的东西:

vb_login_username=<user>&cookieuser=1&vb_login_password=<password>&securitytoken=guest&do=login

然后可以使用与您已有的发布内容类似的内容,只需确保您拥有正确的 URL 即可。在获取查询字符串字节时,我还会使用

UTF8
编码。例如(使用您的原始代码,稍作修改)

var postParams = new[] {
    new HttpParam("vb_login_username", "yourusername"),
    new HttpParam("cookieuser", "1"),
    new HttpParam("vb_login_password", "yourpassword"),
    new HttpParam("securitytoken", "guest"),
    new HttpParam("do", "login"),
};

string url = "http://warriorforum.com/login.php?do=login";
var container = new CookieContainer();
var buffer = Encoding.UTF8.GetBytes(GetQueryString(postParams));

var request = (HttpWebRequest)HttpWebRequest.Create(url);
request.CookieContainer = container;
request.UserAgent = "Mozilla/5.0";
request.Method = "POST";
request.KeepAlive = true;
request.AllowAutoRedirect = true;
request.CookieContainer = container;
request.ContentLength = buffer.Length;
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";

using (var requestStream = request.GetRequestStream())
    requestStream.Write(buffer, 0, buffer.Length);

using (var response = request.GetResponse())
{
    if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.NotModified)
    {
        using (var reader = new StreamReader(response.GetResponseStream()))
        {
            var result = reader.ReadToEnd();
            richTextBox1.Text = result; //this is to read the page source after the request
        }
    }
}

也请注意使用

ContentType
进行的更改。


0
投票

您似乎错过了登录时浏览器执行的操作...该论坛是否真的需要

POST
或者
GET
?你的所有参数都正确吗?当从浏览器登录时,网页是否可能发送附加参数(隐藏)?

当您通过浏览器手动登录时,您需要查看网络上到底发生了什么 - 使用 WiresharkFiddler 找出并模拟代码中发生的情况...

© www.soinside.com 2019 - 2024. All rights reserved.