模拟登录
tring url = "http://my.b2b.hc360.com/my/turbine/template/firstview,other_login.html ";
string indata = @ "LoginID=mytestcs&Passwd=aabbccdd&LoginChk=true&Submit=%B5%C7%A1%A1%A1%A1%C2%BC ";
string outdata= " ";
CookieContainer myCookieContainer=new CookieContainer();
//新建一个CookieContainer来存放Cookie集合
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(url);
//新建一个HttpWebRequest
myHttpWebRequest.ContentType= "application/x-www-form-urlencoded ";
myHttpWebRequest.ContentLength=indata.Length;
myHttpWebRequest.Method= "POST ";
myHttpWebRequest.CookieContainer=myCookieContainer;
//设置HttpWebRequest的CookieContainer为刚才建立的那个myCookieContainer
Stream myRequestStream=myHttpWebRequest.GetRequestStream();
StreamWriter myStreamWriter=new StreamWriter(myRequestStream,Encoding.GetEncoding( "gb2312 "));
myStreamWriter.Write(indata);
//把数据写入HttpWebRequest的Request流
myStreamWriter.Close();
myRequestStream.Close();
//关闭打开对象
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
//新建一个HttpWebResponse
myHttpWebResponse.Cookies=myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
//获取一个包含url的Cookie集合的CookieCollection
Stream myResponseStream=myHttpWebResponse.GetResponseStream();
StreamReader myStreamReader=new StreamReader(myResponseStream,Encoding.GetEncoding( "gb2312 "));
outdata=myStreamReader.ReadToEnd();
//把数据从HttpWebResponse的Response流中读出
myStreamReader.Close();
myResponseStream.Close();
Console.WriteLine(outdata);
//显示 "登录 "
//拿到了Cookie,再进行请求就能直接读取到登录后的内容了
myHttpWebRequest=(HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest.CookieContainer=myCookieContainer;//*
//刚才那个CookieContainer已经存有了Cookie,把它附加到HttpWebRequest中则能直接通过验证
myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
myHttpWebResponse.Cookies=myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
myResponseStream=myHttpWebResponse.GetResponseStream();
myStreamReader=new StreamReader(myResponseStream,Encoding.GetEncoding( "gb2312 "));
outdata=myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
Console.WriteLine(outdata);
string indata = @ "LoginID=mytestcs&Passwd=aabbccdd&LoginChk=true&Submit=%B5%C7%A1%A1%A1%A1%C2%BC ";
string outdata= " ";
CookieContainer myCookieContainer=new CookieContainer();
//新建一个CookieContainer来存放Cookie集合
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(url);
//新建一个HttpWebRequest
myHttpWebRequest.ContentType= "application/x-www-form-urlencoded ";
myHttpWebRequest.ContentLength=indata.Length;
myHttpWebRequest.Method= "POST ";
myHttpWebRequest.CookieContainer=myCookieContainer;
//设置HttpWebRequest的CookieContainer为刚才建立的那个myCookieContainer
Stream myRequestStream=myHttpWebRequest.GetRequestStream();
StreamWriter myStreamWriter=new StreamWriter(myRequestStream,Encoding.GetEncoding( "gb2312 "));
myStreamWriter.Write(indata);
//把数据写入HttpWebRequest的Request流
myStreamWriter.Close();
myRequestStream.Close();
//关闭打开对象
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
//新建一个HttpWebResponse
myHttpWebResponse.Cookies=myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
//获取一个包含url的Cookie集合的CookieCollection
Stream myResponseStream=myHttpWebResponse.GetResponseStream();
StreamReader myStreamReader=new StreamReader(myResponseStream,Encoding.GetEncoding( "gb2312 "));
outdata=myStreamReader.ReadToEnd();
//把数据从HttpWebResponse的Response流中读出
myStreamReader.Close();
myResponseStream.Close();
Console.WriteLine(outdata);
//显示 "登录 "
//拿到了Cookie,再进行请求就能直接读取到登录后的内容了
myHttpWebRequest=(HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest.CookieContainer=myCookieContainer;//*
//刚才那个CookieContainer已经存有了Cookie,把它附加到HttpWebRequest中则能直接通过验证
myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
myHttpWebResponse.Cookies=myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
myResponseStream=myHttpWebResponse.GetResponseStream();
myStreamReader=new StreamReader(myResponseStream,Encoding.GetEncoding( "gb2312 "));
outdata=myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
Console.WriteLine(outdata);