private string getPageContent(string tastKeyword)
{
string htmlcontent = "";
HttpWebRequest request = null;
HttpWebResponse response = null;
string gethost = string.Empty;
CookieContainer cc = new CookieContainer();
string Cookiesstr = string.Empty;
try
{
//第一次POST请求
string postdata = @"username=autumn-iwom&password=trendschina&input=";//模拟请求数据
string LoginUrl = "http://bbs.auto.ifeng.com/logging.php?action=login&loginsubmit=yes";
request = (HttpWebRequest)WebRequest.Create(LoginUrl);//实例化web访问类
request.Method = "POST";//数据提交方式为POST
//模拟头
request.ContentType = "application/x-www-form-urlencoded";
byte[] postdatabytes = Encoding.UTF8.GetBytes(postdata);
request.ContentLength = postdatabytes.Length;
request.Referer = "bbs.auto.ifeng.com";
request.AllowAutoRedirect = false;
request.CookieContainer = cc;
request.KeepAlive = true;
//提交请求
Stream stream;
stream = request.GetRequestStream();
stream.Write(postdatabytes, 0, postdatabytes.Length);
stream.Close();
//接收响应
response = (HttpWebResponse)request.GetResponse();
//保存返回cookie
response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
CookieCollection cook = response.Cookies;
string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);
Cookiesstr = strcrook;
//取第一次GET跳转地址
//StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));
//string content = sr.ReadToEnd();
response.Close();
}
catch (Exception)
{
//第一次POST出错;
}
try
{
gethost = "http://bbs.auto.ifeng.com/search.php?ascdesc=desc&before=&formhash=89f0be48&orderby=lastpost&searchsubmit=true&srchfid%5B%5D=all&srchfilter=all&srchfrom=0&srchtxt={0}&srchtype=title&srchuname=&st=on"; //第一次GET地址
gethost = string.Format(gethost, tastKeyword);
request = (HttpWebRequest)WebRequest.Create(gethost);
request.Method = "POST";
request.KeepAlive = true;
request.Headers.Add("Cookie:" + Cookiesstr);
request.CookieContainer = cc;
request.AllowAutoRedirect = true;
response = (HttpWebResponse)request.GetResponse();
//设置cookie
Cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri);
//取再次跳转链接
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
htmlcontent = sr.ReadToEnd();
request.Abort();
sr.Close();
response.Close();
}
catch (Exception)
{
//第二次POST出错
}
return htmlcontent;
}
post数据小例
最新推荐文章于 2020-05-07 14:19:27 发布