记录下来备忘
1
Code
public static string PostData(string url,string indata,CookieContainer myCookieContainer)
2 {
3 string outdata="";
4 HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(url);
5 myHttpWebRequest.ContentType="application/x-www-form-urlencoded";
6 myHttpWebRequest.ContentLength=indata.Length;
7 myHttpWebRequest.Method="POST";
8 myHttpWebRequest.CookieContainer=myCookieContainer;
9 Stream myRequestStream=myHttpWebRequest.GetRequestStream();
10 StreamWriter myStreamWriter=new StreamWriter(myRequestStream,Encoding.GetEncoding("gb2312"));
11 myStreamWriter.Write(indata);
12 myStreamWriter.Close();
13 myRequestStream.Close();
14 HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
15 myHttpWebResponse.Cookies=myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
16 Stream myResponseStream=myHttpWebResponse.GetResponseStream();
17 StreamReader myStreamReader=new StreamReader(myResponseStream,Encoding.GetEncoding("gb2312"));
18 outdata=myStreamReader.ReadToEnd();
19 myStreamReader.Close();
20 myResponseStream.Close();
21 return outdata;
22 }
public static string PostData(string url,string indata,CookieContainer myCookieContainer)
2 {
3 string outdata="";
4 HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(url);
5 myHttpWebRequest.ContentType="application/x-www-form-urlencoded";
6 myHttpWebRequest.ContentLength=indata.Length;
7 myHttpWebRequest.Method="POST";
8 myHttpWebRequest.CookieContainer=myCookieContainer;
9 Stream myRequestStream=myHttpWebRequest.GetRequestStream();
10 StreamWriter myStreamWriter=new StreamWriter(myRequestStream,Encoding.GetEncoding("gb2312"));
11 myStreamWriter.Write(indata);
12 myStreamWriter.Close();
13 myRequestStream.Close();
14 HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
15 myHttpWebResponse.Cookies=myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
16 Stream myResponseStream=myHttpWebResponse.GetResponseStream();
17 StreamReader myStreamReader=new StreamReader(myResponseStream,Encoding.GetEncoding("gb2312"));
18 outdata=myStreamReader.ReadToEnd();
19 myStreamReader.Close();
20 myResponseStream.Close();
21 return outdata;
22 }
Code
CookieContainer myCookieContainer=new CookieContainer();
string url="?????????????????";//登录页面的链接,看from里面的action
string loginfo="?????????????????";//登录字符串,例如user=??&pass=????
PostData(url,loginfo,myCookieContainer);//这个返回的应该是登录成功的信息,而且写好cookie了
url="???????????";//这个就是想要读取的页面地址,普通情况用StreamReader是读不到的喔
PostData(url,"",myCookieContainer);//返回的就是目标页面,完成,如果还要连续读其它页面,继续这两句代码就行了(cookie已经保存了,自己用就好了).
CookieContainer myCookieContainer=new CookieContainer();
string url="?????????????????";//登录页面的链接,看from里面的action
string loginfo="?????????????????";//登录字符串,例如user=??&pass=????
PostData(url,loginfo,myCookieContainer);//这个返回的应该是登录成功的信息,而且写好cookie了
url="???????????";//这个就是想要读取的页面地址,普通情况用StreamReader是读不到的喔
PostData(url,"",myCookieContainer);//返回的就是目标页面,完成,如果还要连续读其它页面,继续这两句代码就行了(cookie已经保存了,自己用就好了).