C#使用HttpWebRequest与HttpWebResponse模拟用户登录
模拟艺龙旅游网登录
想模拟登录,首先整理一下流程
1.通过360浏览器(IE,火狐等等)F12开发人员工具抓到相关数据
2.获取验证码(拿到cookie),登录时也需要使用
3.登录
-------------------------------
F12调出开发人员工具,输入用户名,密码登录,看我们抓到了什么信息。

Form Data:这个是我们要POST传输的数据:
userName=xzdylyh&passwd=12313&validateCode=验证码&rememberMe=false
其它一些重要信息在Request Headers中
*****************************************************************
我使用C# 设计的winform界面

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Net;
using System.IO;
using System.Data;
namespace HTTPHELPER
{
public class ELOGN_LOGIN
{ public static CookieContainer container = null; //存储验证码cookie #region 登录
public string requestM(string uName,string passwd,string vaildate)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
request = (HttpWebRequest)HttpWebRequest.Create("https://secure.elong.com/passport/ajax/elongLogin");
request.Method = "Post";
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36";
request.AllowAutoRedirect = true;
request.CookieContainer = container;//获取验证码时候获取到的cookie会附加在这个容器里面
request.KeepAlive = true;//建立持久性连接
//整数据
string postData = string.Format("userName={0}&passwd={1}&validateCode={2}&rememberMe=true", uName, passwd, vaildate);
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] bytepostData = encoding.GetBytes(postData);
request.ContentLength = bytepostData.Length; //发送数据 using结束代码段释放
using (Stream requestStm = request.GetRequestStream())
{
requestStm.Write(bytepostData, , bytepostData.Length);
} //响应
response = (HttpWebResponse)request.GetResponse();
string text = string.Empty;
using (Stream responseStm = response.GetResponseStream())
{
StreamReader redStm = new StreamReader(responseStm, Encoding.UTF8);
text = redStm.ReadToEnd();
} return text;
}
catch (Exception ex)
{
var msg = ex.Message;
return msg;
} }
#endregion #region 获取验证码
public Stream getCodeStream(string codeUrl)
{ //验证码请求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(codeUrl);
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0.1) Gecko/20100101 Firefox/5.0.1";
request.Accept = "image/webp,*/*;q=0.8";
request.CookieContainer = new CookieContainer();//!Very Important.!!!
container = request.CookieContainer;
var c = request.CookieContainer.GetCookies(request.RequestUri);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Cookies = container.GetCookies(request.RequestUri); Stream stream = response.GetResponseStream();
return stream;
}
}
#endregion
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using HTTPHELPER;
namespace WindowsFormsApplication8
{
public partial class ELONG_LOGIN_FORM : Form
{
public ELONG_LOGIN_FORM()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{ ELOGN_LOGIN elongLogin = new ELOGN_LOGIN(); var rmsg = elongLogin.requestM(txtuserName.Text,txtPassword.Text,txtVaildata.Text);
MessageBox.Show(rmsg);
} private void ELONG_LOGIN_FORM_Load(object sender, EventArgs e)
{
ReflshPicImage();//更新验证码
} //更新验证码
public void ReflshPicImage()
{
string codeUrl = "https://secure.elong.com/passport/getValidateCode";
ELOGN_LOGIN agent = new ELOGN_LOGIN();
Stream stmImage = agent.getCodeStream(codeUrl);
picValidate.Image = Image.FromStream(stmImage);
} private void btnReValidate_Click(object sender, EventArgs e)
{
ReflshPicImage();//更新验证码
} private void picValidate_Click(object sender, EventArgs e)
{
ReflshPicImage();//更新验证码
}
}
}
最后执行效果,登录的session已经成功返回。

C#使用HttpWebRequest与HttpWebResponse模拟用户登录的更多相关文章
- C# 利用 HttpWebRequest 和 HttpWebResponse 模拟登录有验证码的网站
原文:C# 利用 HttpWebRequest 和 HttpWebResponse 模拟登录有验证码的网站 我们经常会碰到需要程序模拟登录一个网站,那如果网站需要填写验证码的要怎样模拟登录呢?这篇文章 ...
- 运用String类实现一个模拟用户登录程序
package Test; import java.util.Scanner; // 模拟用户登录程序 // 思路: // 1.用两个String类分别接收用户名和密码 // 2.判断输入的用户名和密 ...
- java.net.URL 模拟用户登录网页并维持session
java.net.URL 模拟用户登录网页并维持session 半成品,并非完全有用 import java.io.BufferedReader; import java.io.InputStream ...
- python 初学习 模拟用户登录
#!/usr/bin/env python#coding:utf-8''' 2017年8月19日 模拟用户登录,userfile 文件保存字典 用户名,和密码 sorryname 文件保存字典 登录过 ...
- python3.0 模拟用户登录,三次错误锁定
# -*- coding:utf-8 -*- #需求模拟用户登录,超过三次错误锁定不允许登陆 count = 0 #realname passwd Real_Username = &quo ...
- xpath技术解析xml以及案例模拟用户登录效果
问题:当使用dom4j查询比较深的层次结构的节点(标签,属性,文本),比较麻烦!!! xpath就在此情况下产生了--主要是用于快速获取所需的[节点对象]. 在dom4j中如何使用xPath技术 1) ...
- jdbc封装模拟用户登录
dao层 接口 package com.qu.dao; public interface ILoginDAO { /** * 模拟用户登录 * 验证用户名 密码是否正确 * select * from ...
- scrapy模拟用户登录
scrapy框架编写模拟用户登录的三种方式: 方式一:携带cookie登录,携带cookie一般请求的url为登录后的页面,获取cookie信息应在登录后的页面获取,cookie参数应转成字典形式 # ...
- java.net.URL 模拟用户登录网页并维持session【转】
java.net.URL 模拟用户登录网页并维持session 半成品,并非完全有用 import java.io.BufferedReader; import java.io.InputStream ...
随机推荐
- Win10下安装MySQL5.6
Win10下安装MySQL5.6 我分了两种下载安装的方式给大家看,注意数据库这个东西不在乎版本是不是最新,在乎的是够稳定,现在公司中常用的是mysql5.5和mysql5.6的版本,我现在就用mys ...
- COMSOL
COMSOL_百度百科 https://baike.baidu.com/item/COMSOL/10943148?fr=aladdin 显著特点 ■ 求解多场问题 = 求解方程组,用户只需选择或者自定 ...
- PHP之对象类型
PHP之object对象 对象初始化 要创建一个新的对象object,使用new语句实例化一个类: 转化为对象 如果讲一个对象转化成对象,它将不会有任何变化.如果其它任何类型的值被转化成对象,将会创建 ...
- Java 输入/输出——处理流(ObjectIO)
Object流:直接将Object流写入或读出. TestObjectIO.java transient关键字(英文名:透明的,可以用来修饰成员变量(实例变量),transient修饰的成员变量(实例 ...
- 转:Redis 3.2.1集群搭建
Redis 3.2.1集群搭建 一.概述 Redis3.0版本之后支持Cluster. 1.1.redis cluster的现状 目前redis支持的cluster特性: 1):节点自动发现 2) ...
- LeetCode 976 Largest Perimeter Triangle 解题报告
题目要求 Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero ...
- LeetCode 682 Baseball Game 解题报告
题目要求 You're now a baseball game point recorder. Given a list of strings, each string can be one of t ...
- html 标签笔记
<一.HTML 基础结构> <html>________________________________<head><title>无标题文档</t ...
- 浏览器数据库 IndexedDB 入门教程
一.概述 随着浏览器的功能不断增强,越来越多的网站开始考虑,将大量数据储存在客户端,这样可以减少从服务器获取数据,直接从本地获取数据. 现有的浏览器数据储存方案,都不适合储存大量数据:Cookie 的 ...
- IntelliJ配置SpringMVC提示“found:java.lang.String required:java.lang.String”
File->Invalid Cache&Restart 据说Android Studio也会出现这个问题,但是没遇到过 重启环境后,找不到Controller的问题也解决了