出现问题:
asp.net模拟登陆需要采集的指定页面后(假设为:member/index.aspx)采集不到数据(获取的数据内容老是为:You are not currently connected to the website),从网站登录进去member/index.aspx后再去采集则可以获取想要的Code,
附:登录后刚加载member/index.aspx页面输出的内容是:You are not currently connected to the website,几秒后才开始显示我想要的Code;
asp.net模拟登陆需要采集的指定页面后(假设为:member/index.aspx)采集不到数据(获取的数据内容老是为:You are not currently connected to the website),从网站登录进去member/index.aspx后再去采集则可以获取想要的Code,
附:登录后刚加载member/index.aspx页面输出的内容是:You are not currently connected to the website,几秒后才开始显示我想要的Code;
private string HttpWebRequestLogin(string loginUrl, string postData, ref CookieContainer cookieContainer)
{
byte[] bdata = Encoding.Default.GetBytes(postData);
System.Net.HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(loginUrl);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
myRequest.Referer = loginUrl;
myRequest.KeepAlive = true;
myRequest.AllowAutoRedirect = true;
if (cookieContainer == null)
cookieContainer = new CookieContainer();
myRequest.CookieContainer = cookieContainer;
myRequest.ContentLength = bdata.Length;
Stream newStream = myRequest.GetRequestStream();
newStream.Write(l_data, 0, bdata.Length);
newStream.Close();
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.GetEncoding("GB2312"));
string content = reader.ReadToEnd();
return content;
}
先要登录网站,再采集数据。每次采集,通过Thread实现延迟
void bind()
{
string url = "http://live.arbitragepro.com/modules/arb/index.php";
CookieContainer myCookieContainer = cc;
string referer = "";
string l_ret = "";
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest.ContentType = "text/html";
myHttpWebRequest.Method = "GET";
myHttpWebRequest.Referer = referer;
//myHttpWebRequest.Headers.Add("cookie:" + cookieHeader);
myHttpWebRequest.CookieContainer = myCookieContainer; HttpWebResponse response = null;
System.IO.StreamReader sr = null;
Thread.Sleep(20000);
response = (HttpWebResponse)myHttpWebRequest.GetResponse();
sr = new System.IO.StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312")); // //utf-8
l_ret = sr.ReadToEnd();
// string s = null;
}