C# 百度自动登录器 看看问题
看看哪里有问题,调试运行没问题。可就是不能登录。using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Text;
using System.Net;
using System.IO;
namespace xxx
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
//获取cookie方法
public string GetCookie(string cookieSTR)
{
string result = "";
string[] myArray = cookieSTR.Split(',');
if (myArray.Length > 0)
{
result = "Cookie: ";
foreach (var str in myArray)
{
string[] CookieArray = str.Split(';');
result += CookieArray[0].Trim();
result += "; ";
}
result = result.Substring(0, result.Length - 2);
}
return result;
}
string cookie;
void Button1Click(object sender, EventArgs e)
{
string userid;
string password;
userid=textBox2.Text.ToString();
password=textBox3.Text.ToString();
WebClient client = new WebClient();
//定义webclient
byte[] buff=client.DownloadData("https://passport.baidu.com/v2/?login");
cookie=client.ResponseHeaders.Get("Set-Cookie");
//获取cookie
string CookieSTR=cookie;
//定义cookie
string postURL="https://passport.baidu.com/v2/?login";
//提交地址
client.Headers.Add("Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword , */* Referer: https://passport.baidu.com/v2/?login&tpl=mn Accept-Language: zh-cn Content-Type: application/x-www-form-urlencoded Accept-Encoding: gzip, deflate User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Foxy/1; .NET CLR 2.0.50727) Host: passport.baidu.com Content-Length: 247 Connection: Keep-Alive Cache-Control: no-cache");
//添加头信息
string postVAR="tpl_ok=&next_target=&tpl=mn&skip_ok=&aid=&need_pay=&need_coin=&pay_method=&u=http%3A%2F%2Fwww.baidu.com%2F&return_method=get&more_param=&return_type=&psp_tt=0&password="+password+"&safeflg=0&isphone=tpl&username="+userid+"&verifycode=&mem_pass=on";
//提交密码,账号
byte[] postDATA=System.Text.Encoding.ASCII.GetBytes(postVAR);
//提交
byte[] returnVAR;
//返回
if (CookieSTR!= "")
{
client.Headers.Add("Cookie",CookieSTR);
//添加cookie头
returnVAR =client.UploadData(postURL,"POST",postDATA);
//提交返回
textBox1.Text=System.Text.Encoding.UTF8.GetString(returnVAR);
//显示返回结果
}
else
{
CookieSTR= client.ResponseHeaders["Set-Cookie"].ToString();
CookieSTR = GetCookie(cookie);
//获取cookie
client.Headers.Add("Cookie",CookieSTR);
//添加cookie头
returnVAR =client.UploadData(postURL,"POST",postDATA);
//提交返回
textBox1.Text=System.Text.Encoding.UTF8.GetString(returnVAR);
//显示返回结果
}
}
}
}
--------------------编程问答-------------------- --------------------编程问答--------------------
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Text;
using System.Net;
using System.IO;
namespace xxx
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
//获取cookie方法
public string GetCookie(string cookieSTR)
{
string result = "";
string[] myArray = cookieSTR.Split(',');
if (myArray.Length > 0)
{
result = "Cookie: ";
foreach (var str in myArray)
{
string[] CookieArray = str.Split(';');
result += CookieArray[0].Trim();
result += "; ";
}
result = result.Substring(0, result.Length - 2);
}
return result;
}
string cookie;
void Button1Click(object sender, EventArgs e)
{
string userid;
string password;
userid=textBox2.Text.ToString();
password=textBox3.Text.ToString();
WebClient client = new WebClient();
//定义webclient
byte[] buff=client.DownloadData("https://passport.baidu.com/v2/?login");
cookie=client.ResponseHeaders.Get("Set-Cookie");
//获取cookie
string CookieSTR=cookie;
//定义cookie
string postURL="https://passport.baidu.com/v2/?login";
//提交地址
client.Headers.Add("Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword , */* Referer: https://passport.baidu.com/v2/?login&tpl=mn Accept-Language: zh-cn Content-Type: application/x-www-form-urlencoded Accept-Encoding: gzip, deflate User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Foxy/1; .NET CLR 2.0.50727) Host: passport.baidu.com Content-Length: 247 Connection: Keep-Alive Cache-Control: no-cache");
//添加头信息
string postVAR="tpl_ok=&next_target=&tpl=mn&skip_ok=&aid=&need_pay=&need_coin=&pay_method=&u=http%3A%2F%2Fwww.baidu.com%2F&return_method=get&more_param=&return_type=&psp_tt=0&password="+password+"&safeflg=0&isphone=tpl&username="+userid+"&verifycode=&mem_pass=on";
//提交密码,账号
byte[] postDATA=System.Text.Encoding.ASCII.GetBytes(postVAR);
//提交
byte[] returnVAR;
//返回
if (CookieSTR!= "")
{
client.Headers.Add("Cookie",CookieSTR);
//添加cookie头
returnVAR =client.UploadData(postURL,"POST",postDATA);
//提交返回
textBox1.Text=System.Text.Encoding.UTF8.GetString(returnVAR);
//显示返回结果
}
else
{
CookieSTR= client.ResponseHeaders["Set-Cookie"].ToString();
CookieSTR = GetCookie(cookie);
//获取cookie
client.Headers.Add("Cookie",CookieSTR);
//添加cookie头
returnVAR =client.UploadData(postURL,"POST",postDATA);
//提交返回
textBox1.Text=System.Text.Encoding.UTF8.GetString(returnVAR);
//显示返回结果
}
}
}
}
补充:.NET技术 , C#