利用httpwebrequest登陆csdn出现的问题

vip__888 vip__888 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我利用httpwebrequest登陆老是实现不了。不知道为什么。
我说一下我的流程。

登陆userlogin.aspx页面,将页面的cookie保存,然后使用保存的cookie到验证码页面将验证码下载下来

接着继续利用cookie post数据到表单。
但是没有成功。不知道是什么原因。
代码如下。
        /// <summary>
/// 通过get方式请求页面,传递一个实例化的cookieContainer
/// </summary>
/// <param name="postUrl"></param>
/// <param name="cookie"></param>
/// <returns></returns>
public static ArrayList GetHtmlData(string postUrl, CookieContainer cookie)
{
HttpWebRequest request;
HttpWebResponse response;
ArrayList list = new ArrayList();
request = WebRequest.Create(postUrl) as HttpWebRequest;
request.Method = "GET";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.2; CIBA; .NET4.0C; .NET4.0E)";
request.CookieContainer = cookie;
request.KeepAlive = true;
request.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
try
{
//获取服务器返回的资源
using (response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default))
{
if (response.Cookies.Count > 0)
cookie.Add(response.Cookies);
//保存Cookies
list.Add(cookie);
list.Add(reader.ReadToEnd());
list.Add(Guid.NewGuid().ToString());//图片名
}
}
}
catch (WebException ex)
{
list.Clear();
list.Add("发生异常\n\r");
WebResponse wr = ex.Response;
using (Stream st = wr.GetResponseStream())
{
using (StreamReader sr = new StreamReader(st, System.Text.Encoding.Default))
{
list.Add(sr.ReadToEnd());
}
}
}
catch (Exception ex)
{
list.Clear();
list.Add("5");
list.Add("发生异常:" + ex.Message);
}
return list;
}


/// <summary>
/// 下载验证码图片并保存到本地
/// </summary>
/// <param name="Url">验证码URL</param>
/// <param name="cookCon">Cookies值</param>
/// <param name="savePath">保存位置/文件名</param>
public static bool DowloadCheckImg(string Url, CookieContainer cookCon, string savePath)
{
bool bol = true;
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Url);
webRequest.Referer = "http://passport.csdn.net/UserLogin.aspx";
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.2; CIBA; .NET4.0C; .NET4.0E)";
webRequest.KeepAlive = true;
webRequest.Accept = "*/*";
webRequest.CookieContainer = cookCon;
try
{
//获取服务器返回的资源
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
using (Stream sream = webResponse.GetResponseStream())
{
List<byte> list = new List<byte>();
while (true)
{
int data = sream.ReadByte();
if (data == -1)
break;
list.Add((byte)data);
}
File.WriteAllBytes(savePath, list.ToArray());
}
}
}
catch (WebException ex)
{
bol = false;
}
catch (Exception ex)
{
bol = false;
}
return bol;
}


        /// <summary>
/// 发送相关数据至页面
/// 进行登录操作
/// 并保存cookie
/// </summary>
/// <param name="postData"></param>
/// <param name="postUrl"></param>
/// <param name="cookie"></param>
/// <returns></returns>
public static ArrayList PostData(string postData, string postUrl, CookieContainer cookie)
{
ArrayList list = new ArrayList();
HttpWebRequest request;
HttpWebResponse response;
ASCIIEncoding encoding = new ASCIIEncoding();
request = WebRequest.Create(postUrl) as HttpWebRequest;
byte[] b = encoding.GetBytes(postData);
request.Method = "POST";
request.CookieContainer = cookie;
request.ContentLength = b.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(b, 0, b.Length);
}
try
{
//获取服务器返回的资源
using (response = request.GetResponse() as HttpWebResponse)
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
if (response.Cookies.Count > 0)
cookie.Add(response.Cookies);
list.Add(cookie);
list.Add(reader.ReadToEnd());
}
}
}
catch (WebException wex)
{
WebResponse wr = wex.Response;
using (Stream st = wr.GetResponseStream())
{
using (StreamReader sr = new StreamReader(st, System.Text.Encoding.Default))
{
list.Add(sr.ReadToEnd());
}
}
}
catch (Exception ex)
{
list.Add("发生异常\n\r"+ex.Message);
}
return list;
}


主要的3个方法。不需要登陆的话可以抓取到数据,不知道为什么这样就不可以。
求解。
...全文
评论 27
收藏 已收藏
分享
返回顶部
vip__888
.NET社区 62,272 成员 668,968 内容