我试图登录到已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
}
}
}
请求后的反应是一样的页面,没有什么变化,没有消息告诉我,我输入密码错误或类似的东西。
我只是用我的泛型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
类,这是我一直在写一个库的一部分,该功能是相当交织在一起。 我们希望,但是,它至少给你post数据的想法。 我也包括我自己的班上有几个有用的结构/功能,这应该帮助。 (注意,需要到.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
为好。
你似乎失去了一些东西,当你登录的浏览器不...没有这个论坛确实需要一个POST
或者一个GET
? 都是你的参数是否正确? 登录时,从浏览器是否发生网页或者发送额外的参数(隐藏)?
你需要看看究竟到线路上,当你手动登录通过浏览器-使用Wireshark的或小提琴手找出来,然后模拟代码会发生什么?