HttpWebResponse response = null;  HttpWebRequest request = null;
string wangzhi  = this.textBox1.Text;
wangzhi="http://ts.lobit.cn/educa/unvisity/tvbit/g3shu.php";
request =WebRequest.Create(wangzhi) as HttpWebRequest;
request.Method = "POST";
byte[] buffer = Encoding.GetEncoding("GB2312").GetBytes("myid=caifu100178&pwd=2712120");
request.ContentLength = buffer.Length;
request.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*";
request.ContentType = "application/x-www-form-urlencoded";
request.KeepAlive = true;
request.Referer = "http://ts.lobit.cn/";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; TencentTraveler ; .NET CLR 1.1.4322";
CookieContainer container=new CookieContainer();
container.SetCookies(new Uri(wangzhi), "PHPSESSID=37c1125efc3e209172eb245cdfde1d65");
request.CookieContainer = container;
CredentialCache credCache = new CredentialCache();
NetworkCredential CRED = new NetworkCredential("caifu100178", "2712120");
credCache.Add(new Uri(wangzhi), "Basic", CRED);
Stream w = request.GetRequestStream();
w.Write(buffer, 0, buffer.Length); response = request.GetResponse() as HttpWebResponse;
Stream sr  = response.GetResponseStream();
StreamReader s = new StreamReader(sr, Encoding.GetEncoding("gb2312"));
start: string jy = s.ReadToEnd();
int sindex=0,  eindex=0, i=0;
for(i=0; i<1000;i++)
{

sindex=jy.IndexOf("rtsp://",sindex); if (sindex!=-1)
{
eindex = jy.IndexOf("rm", sindex, 100);
if (sindex!=-1 && eindex!=-1 && eindex>sindex)
{
string a = jy.Substring(sindex, eindex - sindex + 2);
DirectoryInfo MuLu =Directory.CreateDirectory("E:\\jiaoyu");
if(!MuLu.Exists)
{MuLu.Create();}
DirectoryInfo MuLus =Directory.CreateDirectory("E:\\jiaoyu\\"+wangzhi.Replace("http://ts.lobit.cn/educa/unvisity/tvbit/",""));
FileInfo myfile =new FileInfo("E:\\jiaoyu\\"+wangzhi.Replace("http://ts.lobit.cn/educa/unvisity/tvbit/","") +"\\wangzhi.txt");
           
StreamWriter sw = myfile.AppendText();
sw.WriteLine(a);
sw.Close();
sindex = sindex + 1;
}
}
else{
int xindex=0,  yindex=0, j=0; 

for(j=0; j< 1000;j++)
{
xindex = xindex + 1; xindex =( jy.IndexOf("href=\"",xindex) + "href=\"".Length);
yindex = jy.IndexOf("php", xindex, 15); if (yindex > xindex)
{
wangzhi = jy.Substring(xindex, yindex - xindex);
wangzhi = "http://ts.lobit.cn/educa/unvisity/tvbit/" + wangzhi + "php";
request =WebRequest.Create(wangzhi) as HttpWebRequest;
request.Method = "get";
request.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*";
request.ContentType = "application/x-www-form-urlencoded";
request.KeepAlive = true;
request.Referer = "http://ts.lobit.cn/member/index.php";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; TencentTraveler ; .NET CLR 1.1.4322";
CookieContainer container1=new CookieContainer();
container.SetCookies(new Uri(wangzhi), "PHPSESSID=37c1125efc3e209172eb245cdfde1d65");
request.CookieContainer = container1;Stream w1 = request.GetRequestStream();
response = request.GetResponse() as HttpWebResponse;
Stream sr1  = response.GetResponseStream();
StreamReader s1 = new StreamReader(sr, Encoding.GetEncoding("gb2312"));
goto start; } }


}我想从网站时的每个网页中包含的视频文件抓下来,当第一次POST帐号和密码和COOKIE上去,可以抓下内容来,然后转向下一个网页抓取,这时问题出现了,超时退出!不管是该网站什么地址,只要输进去,第一页总能抓下来,再继续抓第二页时,总是抓不下来!超时
    有没有高手帮我分析一下,拜托!!!!!!!!!!!!!!

解决方案 »

  1.   

    你这问题我觉得很小,你每次只抓一个页面,然后再登陆,再抓取不就完了。另外,你对cookiecontainer的处理,我觉得只要在response之后记录一下request的cookiecontainer就行了,在请求具体页面的时候加上这个cookiecontainer就可以访问这个页面了。
      

  2.   

    1,用httpwatch分析登陆post数据和返回的cookie,看看是不是你漏了什么2,你的程序似乎没有保存第一次登陆时候获取的Cookie,没有第一次返回的cookie,你第二次怎么能够取得网页?3,网页抓取经常是一些小问题出错,如果你怕麻烦,看看CSDN小助手的源码,那里面有现成的类