winfrm模拟自动登录错误,高手请进。
每次登陆http://www2.baidu.com/太麻烦,做了个模拟自动登陆的跳转到另外一页http://www2.baidu.com/query/query_list.php并取html源码,但是有错误,错误在程序中已标出。那位大侠帮帮忙改改。
protected static string cookieheader;
private void Form1_Load(object sender, EventArgs e)
{
string param = "Action=&entered_login=luo&entered_password=123&entered_imagecode=ohtm "
byte[] bs = Encoding.UTF8.GetBytes(param);
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create( "http://www2.baidu.com/ ");
req.Method = "POST ";
req.AllowAutoRedirect = true;
req.ContentType = "application/x-www-form-urlencoded ";
req.ContentLength = bs.Length;
CookieContainer cookieCon = new CookieContainer();
req.CookieContainer = cookieCon;
using (Stream reqStream = req.GetRequestStream())
{
//req.Timeout = 3000;
reqStream.Write(bs, 0, bs.Length);
HttpWebResponse res = res = (HttpWebResponse)req.GetResponse();
cookieheader = req.CookieContainer.GetCookieHeader(new Uri( "http://www2.baidu.com/ "));
HttpContext.Current.Application.Lock();//错误为未将对象引用到实列
HttpContext.Current.Application[ "cookieheader "] = cookieheader;
HttpContext.Current.Application.UnLock();
}
using (WebResponse wr = req.GetResponse())
{
StreamReader reader = new StreamReader(wr.GetResponseStream(), Encoding.Default);
richTextBox1.Text = reader.ReadToEnd();
}
getpage();//用已有的cookie身份跳转的另外一页
}
public static string getPage()
{
HttpWebResponse res = null;
string strResult = " ";
try
{
string param = "Action=&entered_login=luo&entered_password=123&entered_imagecode=ohtm "
string url= "http://www2.baidu.com/query/query_list.php ";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST ";