我是靠谱客的博主 诚心冷风,最近开发中收集的这篇文章主要介绍http://bbs.hc360.com/forum.php,获取网站中response.Headers["location"],找到抓取页面 | 学步园...,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

private static 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 post = @"formhash=59f68caf&srchtxt={0}&searchsubmit=yes";//模拟请求数据

string postdata = string.Format(post, tastKeyword);

string LoginUrl = "http://bbs.hc360.com/search.php?mod=forum";

request = (HttpWebRequest)WebRequest.Create(LoginUrl);//实例化web访问类

request.Method = "POST";//数据提交方式为POST

//模拟头

request.ContentType = "application/x-www-form-urlencoded";

byte[] postdatabytes = Encoding.GetEncoding("gbk").GetBytes(postdata);

request.ContentLength = postdatabytes.Length;

request.Referer = "http://bbs.hc360.com/search.php?mod=forum";

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

{

string loction = response.Headers["location"];

gethost = "http://bbs.hc360.com/" + loction; //第一次GET地址

request = (HttpWebRequest)WebRequest.Create(gethost);

request.Method = "GET";

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("gbk"));

htmlcontent = sr.ReadToEnd();

request.Abort();

sr.Close();

response.Close();

}

catch (Exception)

{

//第二次POST出错

}

return htmlcontent;

}

最后

以上就是诚心冷风为你收集整理的http://bbs.hc360.com/forum.php,获取网站中response.Headers["location"],找到抓取页面 | 学步园...的全部内容,希望文章能够帮你解决http://bbs.hc360.com/forum.php,获取网站中response.Headers["location"],找到抓取页面 | 学步园...所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部