httpwebrequest 登录采集

闲游四疯 2017-10-20 03:21:05
winfrom 去获取一个网页源代码 需要先登,自己代码怎么试都不行,谁有可用的代码发个或者帮我分析下是不是哪里做错了?
思路就是先post登录 获取CookieContainer,然后传到下一个请求,获取实际要的源代码?谁能帮我分析下 谢谢


CookieContainer cookie = new CookieContainer();
UtilsRequest.GetPostCookie("http://www.uu354.com/login.aspx", parms,ref cookie);
richTextBox3.Text = UtilsRequest.GetPost("http://www.uu354.com/Member/DanHaoSearch.aspx", null, cookie);

public static string GetPostCookie(string url, Dictionary<string, object> parms, ref CookieContainer cookie)
{
string htmlCode = String.Empty;
HttpWebRequest webRequest = null;
HttpWebResponse webResponse = null;
try
{
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] postData = encoding.GetBytes(GetPostParms(parms));

Uri uri = new Uri(url);
webRequest = System.Net.WebRequest.Create(uri) as HttpWebRequest;
webRequest.Timeout = 30000;
webRequest.Method = "POST";
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36";
webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = postData.Length;
webRequest.CookieContainer = cookie;
webRequest.AllowAutoRedirect = true;
webRequest.KeepAlive = true;

Stream outputStream = webRequest.GetRequestStream();
outputStream.Write(postData, 0, postData.Length);
outputStream.Close();

webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();
webResponse.Cookies = webRequest.CookieContainer.GetCookies(webRequest.RequestUri);

using (StreamReader sr = new StreamReader(webResponse.GetResponseStream(), GetEncoding(webResponse)))
{
htmlCode = sr.ReadToEnd();
}
}
catch { }
finally
{
if (webRequest != null) webRequest.Abort();
if (webResponse != null) webResponse.Close();
}

return htmlCode;
}


http://www.uu354.com/login.aspx



...全文
263 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

110,995

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧