C# 模拟登陆并继续访问其他页面

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;

namespace ConsoleApplication4
{

class Program
{
static void Main(string[] args)
{
//测试下面的信息在F12里都有
HttpHeader header = new HttpHeader();
header.accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
header.contentType = "application/x-www-form-urlencoded";//这个F12里没有,这个一定要,这个是请求头的内容类型,不然参数无法传过去。
header.method = "POST";
header.userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36";
header.maxTry = 300;

string postString = "LoginName=admin&LoginPwd=123456";//这里即为传递的参数,可以用工具抓包分析,也可以自己分析,主要是form里面每一个name都要加进来

string html = HTMLHelper.GetHtml("http://localhost:12055/Admin/Home/MainForm", HTMLHelper.GetCooKie("http://localhost:12055/Admin/Login/CheckLogin", postString, header), header);

Console.WriteLine(html);

Console.ReadKey();
}

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; }
}

}
}

时间: 2024-08-27 15:34:20

C# 模拟登陆并继续访问其他页面的相关文章

python3下scrapy爬虫(第五卷:利用cookie模拟登陆抓取个人中心页面)

之前我们爬取的都是那些无需登录就要可以使用的网站但是当我们想爬取自己或他人的个人中心时就需要做登录,一般进入登录页面有两种 ,一个是独立页面登陆,另一个是弹窗,我们先不管验证码登陆的问题 ,现在试一下直接登陆的爬取: 爬虫是模拟人的行为来请求网页读取数据的现在我们划分一下过程,从登陆到获取: 先看一下我们到个人中心的过程: 登陆界面->输入账号密码->进入个人中心 1 进入登陆页面 可以说是第一次请求 此时会产生相应的COOKIE值,因为你只要先进入到页面才可以进行密码输入等行为 cookie

python爬虫学习(3)_模拟登陆

1.登陆超星慕课,chrome抓包,模拟header,提取表单隐藏元素构成params. 主要是验证码图片地址,在js中发现由js->new Date().getTime()时间戳动态生成url,python对应time.time(),生成验证码图片url,图片下载在本地,手动输入.代码如下: #coding=utf-8 import requests import time from bs4 import BeautifulSoup header={ 'Referer':'http://aus

使用C#的HttpWebRequest模拟登陆访问人人网(转)

无论使用任何语言做模拟登陆或者抓取访问页面,无外乎以下思路:第一 启用一个web访问会话方法或者实例化一个web访问类,如.net中的HttpWebRequest:第二 模拟POST或者GET方式提交的数据:第三 模拟请求的头:第四 提交请求并获得响应,及对响应做我们所需要的处理.这里我们以人人网的登录为例,将涉及到POST以及GET两种请求方式.在之前的文章<免费网页抓包工具,火狐插件FireBug的抓包使用教程>中我们知道,登陆人人网的时候,一共做了一个POST请求以及两个GET请求,如下

模拟登陆、Cookie传递

---恢复内容开始--- 上个月月底把模拟登陆的问题解决了,现在有时间就正好记录并复习一下. 主要使用httpclient 来进行模拟登陆 首先做一个登陆布局  一直以来都是简单拖几个控件 没有啥特效  这次我想使用一点 Material Design设计风格    查了一下 TextInputLayout这个控件  还不错 TextInputLayout 来自于 Android Design Support Library    可以让edittext的hint在用户输入的时候  自动跳到上方

使用HttpWebRequest模拟登陆阿里巴巴(alibaba、httpwebrequest、login)

前言 其实老喜欢取经,偶尔也得分享下.关于阿里巴巴国际站的登陆,过程有点复杂但是算不上难.一不小心少个东西倒也挺麻烦的. 主要是看下请求类HttpClient基本请求封装使用,AliClient模拟浏览器的操作与数据封装 这里只是简单说一下主要的类和注意点,主要步骤与注意点都写在代码注释里了.项目源码下载地址:http://git.oschina.net/GspringG/AliLogin 正文 主要类/方法 HttpClient请求模拟的基础类,也就那么个过程http header设置一下,然

Python实现网站模拟登陆

一.实验简介 1.1 基本介绍 本实验中我们将通过分析登陆流程并使用 Python 实现模拟登陆到一个实验提供的网站,在实验过程中将学习并实践 Python 的网络编程,Python 实现模拟登陆的方法,使用 Firefox 抓包分析插件分析网络数据包等知识. 模拟登录可以帮助用户自动化完成很多操作,在不同场合下有不同的用处,无论是自动化一些日常的繁琐操作还是用于爬虫都是一项很实用的技能.本课程通过 Firefox 和 Python 来实现,环境要求如下: Python 库:urllib, ur

Python模拟登陆练习——imooc.com登陆

写下这篇文章的时候,是博主学习python的第三天( 也许是第四天:( ),python是博主接触的第二门解释型语言(第一门是javascript). 讲真在很久之前就想要用博客记录自己的学习历程了,然而就像写日记一样,写着写着就放弃了-.- so今天决定给自己一个好的开端~ 博主的学习方式是直奔目的,遇到问题百度各种博客,网站,百度找不到google找,就这样.这种学习方式是真的见效快,但显而易见,基础会比较薄弱. 因此学习python的基本语法,就直奔爬虫了! ---------------

Jsoup实现java模拟登陆

Jsoup实现java模拟登陆 1:如何获取cookies. 1.1:由于需要登录,故先模拟登陆时的那一个<form>,这里用map来装载变量名称,变量值. Map<String, String> map = new HashMap<String, String>(); map.put("username", username); map.put("pwd", md5.getMD5ofStr(password)); map.put

模拟登陆CSDN——就是这么简单

工具介绍 本篇文章主要是讲解如何模拟登陆CSDN,使用的工具是HttpClient+Jsoup 其中HttpClient主要是负责发送请求,而Jsoup主要是解析HTML 你可能对HttpClient的API不太了解,不过没关系,往下看就好了~ Jsoup的语法类似jQuery的选择器,相信有一定web基础的人都可以很快的掌握 其中select(String selector)就是最强大的选择器,另外还提供一系列的细化的方法,比如: getElementById(String id), getE