oRequest.ContentType="application/x-www-form-urlencoded";string postData="user=xxx&pwd=111"; byte[] mybyte=System.Text.Encoding.GetEncoding("GB2312").GetBytes(postData);//最终编码后要上传的数据 oRequest.ContentLength=postData.Length; // Set the content type of the data being posted. oRequest.Method="POST";//post上传方式 // Set the content length of the string being posted. CookieContainer cookieCon = new CookieContainer(); oRequest.CookieContainer = cookieCon;Stream newStream=oRequest.GetRequestStream(); newStream.Write(mybyte,0,mybyte.Length); newStream.Close();
oResponse= (HttpWebResponse)oRequest.GetResponse(); cc=oResponse.Cookies;//用变量保存你的登陆后的cookies2.HttpWebRequest oRequest= (HttpWebRequest)WebRequest.Create("你要抓的页");oRequest.ContentType="application/x-www-form-urlencoded"; // string postData=""; byte[] mybyte=System.Text.Encoding.GetEncoding("GB2312").GetBytes(postData);//最终编码后要上传的数据 oRequest.ContentLength=postData.Length; // Set the content type of the data being posted. oRequest.Method="POST";//post上传方式Stream newStream=oRequest.GetRequestStream(); newStream.Write(mybyte,0,mybyte.Length); newStream.Close();CookieContainer cookieCon = new CookieContainer(); oRequest.CookieContainer = cookieCon;cookieCon.Add(oRequest.RequestUri,cc);//添加登陆后访问的cookies
HttpWebResponse oResponse = (HttpWebResponse)oRequest.GetResponse(); StreamReader oReader = new StreamReader(oResponse.GetResponseStream(),System.Text.Encoding.GetEncoding("GB2312"));
这样应该怎么做?
这遍文章可能对你有用,我用他实现了cookie登陆抓取
我的登录后抓取的代码,你参考参考
1.先到登陆的页
CookieCollection cc=null;//获取一次就可以了
oRequest= (HttpWebRequest)WebRequest.Create("www.xxx.com/login.php");
oRequest.ContentType="application/x-www-form-urlencoded";string postData="user=xxx&pwd=111";
byte[] mybyte=System.Text.Encoding.GetEncoding("GB2312").GetBytes(postData);//最终编码后要上传的数据
oRequest.ContentLength=postData.Length;
// Set the content type of the data being posted.
oRequest.Method="POST";//post上传方式
// Set the content length of the string being posted.
CookieContainer cookieCon = new CookieContainer();
oRequest.CookieContainer = cookieCon;Stream newStream=oRequest.GetRequestStream();
newStream.Write(mybyte,0,mybyte.Length);
newStream.Close();
oResponse= (HttpWebResponse)oRequest.GetResponse();
cc=oResponse.Cookies;//用变量保存你的登陆后的cookies2.HttpWebRequest oRequest= (HttpWebRequest)WebRequest.Create("你要抓的页");oRequest.ContentType="application/x-www-form-urlencoded";
//
string postData="";
byte[] mybyte=System.Text.Encoding.GetEncoding("GB2312").GetBytes(postData);//最终编码后要上传的数据
oRequest.ContentLength=postData.Length;
// Set the content type of the data being posted.
oRequest.Method="POST";//post上传方式Stream newStream=oRequest.GetRequestStream();
newStream.Write(mybyte,0,mybyte.Length);
newStream.Close();CookieContainer cookieCon = new CookieContainer();
oRequest.CookieContainer = cookieCon;cookieCon.Add(oRequest.RequestUri,cc);//添加登陆后访问的cookies
HttpWebResponse oResponse = (HttpWebResponse)oRequest.GetResponse();
StreamReader oReader = new StreamReader(oResponse.GetResponseStream(),System.Text.Encoding.GetEncoding("GB2312"));
string tempstr=oReader.ReadToEnd();
上面的代码你可以试试
我觉得对方网站可能使用了这样一段代码
<SCRIPT>if(self == top) location='exit.php';</SCRIPT>
用以确定网页的查看是否在框架内
这样怎么办呢??
你登陆后不也可以直接打开body.php吗
我从临时文件夹中找到了body.html,跟网站上的body.php显示内容一致,我查看它的源代码后,发现有<SCRIPT>if(self == top) location='exit.php';</SCRIPT>,如果直接用IE打开body.html会提示找不到exit.php,如果删掉这句就可以在IE显示
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
<script language="javascript">
<!--
if(self == top) location='exit.php';
}
//-->
</script>
<meta http-equiv="refresh" content="180">
</head><body>
<!--
if(self == top) location='exit.php';
}
//-->
</script>这样的代码了,下面就是你要抓取的内容了呀,目的已达,还有什么问题呢?
<!--
if(self == top) location='exit.php';
}
//-->
这句代码我是在internet临时文件中的BODY.htm看到的,这个页面显示的内容虽然跟我想抓取的body.php的内容一致~可是这文件body.html只有到临时文件中才找到~也就是说并没有进行采集~
采集时~会因为执行这句语句而退出到登录页面
CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
速度快;使用方便;提供源代码。
界面:http://blog.csdn.net/Qqwwee_Com/category/146601.aspx
下载:http://szlawbook.com/csdnv2
关注这个问题!
都可以下啊。====CSDN 小助手 V2.5 2005年11月05日发布====
CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
界面:http://blog.csdn.net/Qqwwee_Com/archive/2005/11/05/523395.aspx
下载:http://szlawbook.com/csdnv2