110,570
社区成员
发帖
与我相关
我的任务
分享
下面这段代码!老是post登陆不成功!!易语言里面有个post模块 可以成功!!为什么在C#里面成功不了呢?请指教 谢谢!
public static bool LoadWeb(ref string Name,string Pass)
{
WebClient Wc = new WebClient();
Wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
byte[] data = Encoding.ASCII.GetBytes("loginname=FZZhlxWkI@163.com&loginpass=123456&action=login&token=8fb2b717");
data = Wc.UploadData("http://fanfou.com/login", data);
string Code= Encoding.UTF8.GetString(data);
System.Windows.Forms.MessageBox.Show(Code);//这里判断是否正常登陆网站!!
}
测试代码编辑
//这是Post登陆的代码
public static bool LoadWeb(ref string Name,string Pass)
{
string UserName = "FZZhlxWkI@163.com";
string PassWord = "123456";
string Ck = null;
string Token = GetToken(ref Ck);//这个是利用正则表达式提取Token的子程序
//System.Windows.Forms.Application.DoEvents();
//System.Windows.Forms.MessageBox.Show(Token);
string Args = "loginname={0}&loginpass={1}&action=login&token={2}";
Args = string.Format(Args, System.Web.HttpUtility.UrlDecode(UserName), PassWord, Token);
WebClient Wc = new WebClient();
Wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
Wc.Headers.Add("Cookie", Ck);
byte[] data = Encoding.ASCII.GetBytes(Args);
data = Wc.UploadData("http://fanfou.com/login", data);
string Code= Encoding.UTF8.GetString(data);
System.Windows.Forms.MessageBox.Show(Code);//查看返回的文件就可以知道是否登录成功!
}
//下面是GetToken子程序的代码
public static string GetToken(ref string CK)
{
WebClient Wb = new WebClient();
System.Text.RegularExpressions.Regex bds = new System.Text.RegularExpressions.Regex("token\" value=\"(.*?)\"");
byte[] data = Wb.DownloadData("http://fanfou.com/login");
System.Windows.Forms.Application.DoEvents();
String cookie = Wb.ResponseHeaders["Set-Cookie"];
CK= cookie;//获取到Cookie
string Code= Encoding.UTF8.GetString(data);
System.Text.RegularExpressions.Match Mt= bds.Match(Code);
return Mt.Value.Replace("token\" value=\"", null).Replace("\"",null);
}