模拟艺龙旅游网登录

想模拟登录,首先整理一下流程

1.通过360浏览器(IE,火狐等等)F12开发人员工具抓到相关数据

2.获取验证码(拿到cookie),登录时也需要使用

3.登录

-------------------------------

F12调出开发人员工具,输入用户名,密码登录,看我们抓到了什么信息。

Request URL:这个就是登录请求的url  
https://secure.elong.com/passport/ajax/elongLogin
方式POST

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模拟用户登录的更多相关文章

  1. C# 利用 HttpWebRequest 和 HttpWebResponse 模拟登录有验证码的网站

    原文:C# 利用 HttpWebRequest 和 HttpWebResponse 模拟登录有验证码的网站 我们经常会碰到需要程序模拟登录一个网站,那如果网站需要填写验证码的要怎样模拟登录呢?这篇文章 ...

  2. 运用String类实现一个模拟用户登录程序

    package Test; import java.util.Scanner; // 模拟用户登录程序 // 思路: // 1.用两个String类分别接收用户名和密码 // 2.判断输入的用户名和密 ...

  3. java.net.URL 模拟用户登录网页并维持session

    java.net.URL 模拟用户登录网页并维持session 半成品,并非完全有用 import java.io.BufferedReader; import java.io.InputStream ...

  4. python 初学习 模拟用户登录

    #!/usr/bin/env python#coding:utf-8''' 2017年8月19日 模拟用户登录,userfile 文件保存字典 用户名,和密码 sorryname 文件保存字典 登录过 ...

  5. python3.0 模拟用户登录,三次错误锁定

    # -*- coding:utf-8 -*- #需求模拟用户登录,超过三次错误锁定不允许登陆     count = 0   #realname passwd Real_Username = &quo ...

  6. xpath技术解析xml以及案例模拟用户登录效果

    问题:当使用dom4j查询比较深的层次结构的节点(标签,属性,文本),比较麻烦!!! xpath就在此情况下产生了--主要是用于快速获取所需的[节点对象]. 在dom4j中如何使用xPath技术 1) ...

  7. jdbc封装模拟用户登录

    dao层 接口 package com.qu.dao; public interface ILoginDAO { /** * 模拟用户登录 * 验证用户名 密码是否正确 * select * from ...

  8. scrapy模拟用户登录

    scrapy框架编写模拟用户登录的三种方式: 方式一:携带cookie登录,携带cookie一般请求的url为登录后的页面,获取cookie信息应在登录后的页面获取,cookie参数应转成字典形式 # ...

  9. java.net.URL 模拟用户登录网页并维持session【转】

    java.net.URL 模拟用户登录网页并维持session 半成品,并非完全有用 import java.io.BufferedReader; import java.io.InputStream ...

随机推荐

  1. process 多进程写法 multiprocessing

    from multiprocessing import Process def   f1(n):#普通 print(f1) if __name__ == '__main__': lst = [] fo ...

  2. 【视频】dx dy的意思 微分的定义 导数符号的意思

    视频解说 http://www.bilibili.com/video/av8565224/

  3. 自动化运维工具-pssh工具安装配置及简单使用讲解

    1.先决条件:安装pssh工具要求python版本大于2.4即可. 安装pssh工具的主机针对远程主机需要配置免秘钥认证: ssh-keygen -t rsa ssh-copy-id [remoteh ...

  4. OC变量限定符和属性限定符

    ARC当中变量的四种生命周期限定符 __strong __weak __unsafe_unretained __autoreleaseing __strong 是默认的限定符,无需显示引入,只要有强引 ...

  5. 如何实现浏览器向服务器伪造refer?

    Request URL:https://www.getwnmp.org/ Request Method:GET Status Code:304 Remote Address:45.55.134.251 ...

  6. [administrator][netctl] 给未插线未UP端口设置IP

    以下内容均为使用netctl配置工具前提下: 需求: Tstation管理口做日常使用.没有千兆交换.所以加一个一块千兆的卡.这块卡是为了做数据传输专用的. 目前主要就是每周给T7备份使用.但是由于是 ...

  7. mysql windows开启客户端连接权限

    use mysql;  select 'host' from user where user='root';  update user set host = '%' where user ='root ...

  8. 通过pytty工具代理连接数据库mysql(绕开数据库白名单限制)

    1.下载putty在本机,不用安装. 2.数据库地址及端口,输入服务器账户.密码登录. 输入数据库地址.端口及本机映射端口 输入服务器账户.密码登录 然后本地新建数据库连接就可以了

  9. Vue中父子组件执行的先后顺序

    Vera   Vue中父子组件执行的先后顺序探讨(转载) 前几天,朋友向我提出了一个关于Vue中父子组件执行的先后顺序问题,相信很多朋友在学习的过程中也会遇到这个问题,所以我就在此提出我自己的一些小看 ...

  10. java JDBC (七) org.apache.commons.dbutils 查询

    package cn.sasa.demo1; import java.sql.Connection; import java.sql.SQLException; import java.util.Li ...