使用C#的HttpWebRequest模拟登陆网站

news/2024/7/3 2:27:22

很久没有写新的东西了,今天在工作中遇到的一个问题,感觉很有用,有种想记下来的冲动。

这篇文章是有关模拟登录网站方面的。

实现步骤;

  1. 启用一个web会话
  2. 发送模拟数据请求(POST或者GET)
  3. 获取会话的CooKie 并根据该CooKie继续访问登录后的页面,获取后续访问的页面数据。

我们以登录人人网为例,首先需要分析人人网登录时POST的数据格式,这个可以通过IE9中只带的F12快捷键,调出开发人员工具。如下图:

 

通过开始捕获得到POST的地址和POST的数据

POST数据:

email=aaa@163.com&password=111&icode=&origURL=http%3A%2F%2Fwww.renren.com%2Fhome&domain=renren.com&key_id=1&_rtk=90484476

POST地址:

http://www.renren.com/PLogin.do

下面就是代码示例来得到登录后页面(http://guide.renren.com/guide)的数据

HTMLHelper类

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Threading;

namespace Test
{
public class HTMLHelper
{
/// <summary>
/// 获取CooKie
/// </summary>
/// <param name="loginUrl"></param>
/// <param name="postdata"></param>
/// <param name="header"></param>
/// <returns></returns>
public static CookieContainer GetCooKie(string loginUrl, string postdata, HttpHeader header)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
CookieContainer cc = new CookieContainer();
request = (HttpWebRequest)WebRequest.Create(loginUrl);
request.Method = header.method;
request.ContentType = header.contentType;
byte[] postdatabyte = Encoding.UTF8.GetBytes(postdata);
request.ContentLength = postdatabyte.Length;
request.AllowAutoRedirect = false;
request.CookieContainer = cc;
request.KeepAlive = true;

//提交请求
Stream stream;
stream = request.GetRequestStream();
stream.Write(postdatabyte, 0, postdatabyte.Length);
stream.Close();

//接收响应
response = (HttpWebResponse)request.GetResponse();
response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);

CookieCollection cook = response.Cookies;
//Cookie字符串格式
string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);

return cc;
}
catch (Exception ex)
{

throw ex;
}
}

/// <summary>
/// 获取html
/// </summary>
/// <param name="getUrl"></param>
/// <param name="cookieContainer"></param>
/// <param name="header"></param>
/// <returns></returns>
public static string GetHtml(string getUrl, CookieContainer cookieContainer,HttpHeader header)
{
Thread.Sleep(1000);
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null;
try
{
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(getUrl);
httpWebRequest.CookieContainer = cookieContainer;
httpWebRequest.ContentType = header.contentType;
httpWebRequest.ServicePoint.ConnectionLimit = header.maxTry;
httpWebRequest.Referer = getUrl;
httpWebRequest.Accept = header.accept;
httpWebRequest.UserAgent = header.userAgent;
httpWebRequest.Method = "GET";
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
string html = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
httpWebRequest.Abort();
httpWebResponse.Close();
return html;
}
catch (Exception e)
{
if (httpWebRequest != null) httpWebRequest.Abort();
if (httpWebResponse != null) httpWebResponse.Close();
return string.Empty;
}
}
}

public class HttpHeader
{
public string contentType { get; set; }

public string accept { get; set; }

public string userAgent { get; set; }

public string method{get;set;}

public int maxTry { get; set; }
}
}
复制代码

 

测试用例:

复制代码
  HttpHeader header = new HttpHeader();
header.accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";
header.contentType = "application/x-www-form-urlencoded";
header.method = "POST";
header.userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
header.maxTry = 300;

string html = HTMLHelper.GetHtml("http://guide.renren.com/guide", HTMLHelper.GetCooKie("http://www.renren.com/PLogin.do",
"email=aaa@163.com&password=111&icode=&origURL=http%3A%2F%2Fwww.renren.com%2Fhome&domain=renren.com&key_id=1&_rtk=90484476", header), header);

Console.WriteLine(html);


Console.ReadLine();
复制代码

 

转载于:https://www.cnblogs.com/opop/p/5445295.html


http://lihuaxi.xjx100.cn/news/268347.html

相关文章

编解码概念

H.264 H.265 MJPEG :视频编解码规范分析fps(frame per second) : 帧率

Linux内核及发行版

Linux内核及发行版 1. Linux内核 Linux内核是操作系统内部操作和控制硬件设备的核心程序&#xff0c;它是由芬兰人林纳斯开发的。【git也是他发明的】 内核效果图: 【内核在操作系统里面】 说明: 真正操作和控制硬件是由内核来完成的&#xff0c;操作系统是基于内核开发出来…

一步步构建大型网站架构

之前我简单向大家介绍了各个知名大型网站的架构&#xff0c;MySpace的五个里程碑、Flickr的架构、YouTube的架构、PlentyOfFish的架构、WikiPedia的架构。这几个都很典型&#xff0c;我们可以从中获取很多有关网站架构方面的知识&#xff0c;看了之后你会发现你原来的想法很可能…

计算机视觉分析:传统视觉VS深度学习

近日&#xff0c;来自麻省理工学院、加州大学伯克利分校、伊利诺伊大学香槟分校、华盛顿大学、帝国理工学院的六名顶级人工智能科学家、计算机视觉科学家在 ICCV 大会期间进行了题为「A discussion about deep learning vs classical methods and their roles in computer visi…

这个假发太逼真!GAN 帮你换发型,alignment 步骤去掉生硬感

点击上方“视学算法”&#xff0c;选择加"星标"或“置顶”重磅干货&#xff0c;第一时间送达转自 | 新智元来源 | GitHub编辑 | 小匀Tony老师不能给你的&#xff0c;GAN给你&#xff01;这个名为Barbershop的技术能帮你尝试不同的发型和发色。重点是&#xff1a;新发…

IDEA中的.iml文件和.idea文件夹

.iml文件 iml文件是IntelliJ IDEA自动创建的模块文件&#xff0c;用于Java应用开发&#xff0c;存储一些模块开发相关的信息&#xff0c;比如一个Java组件&#xff0c;插件组件&#xff0c;Maven组件等等&#xff0c;还可能存储一些模块路径信息&#xff0c;依赖信息以及别的一…

微型计算机的EMC设计视频,干货 | EMC设计有多难,看完这31个电磁兼容标准电路,瞬间懂了...

1、AC24V接口EMC设计标准电路2、AC110V-220VEMC设计标准电路3、AC380V接口EMC设计标准电路4、AV接口EMC设计标准电路5、CAN接口EMC设计标准电路6、DC12V接口EMC设计标准电路7、DC24V接口EMC设计标准电路8、DC48接口EMC设计标准电路9、DC110V接口EMC设计标准电路10、DVI EMC设计…

archlinux mariadb躺坑

systemctl start mariadb :(AUTHENTICATING FOR org.freedesktop.systemd1.manage-units 启动“mariadb.service”需要认证。 Authenticating as: andrew Password: AUTHENTICATION COMPLETE andrewmanjaro ~ %…