HttpWebRequest和HttpWebResponse类是用于发送和接收HTTP数据的最好选择。它们支持一系列有用的属性。

模拟艺龙旅游网登录

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

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

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

3.登录

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

F12调出开发人员工具,输入用户名,密码登录,看我们抓到了什么信息。 (此处为360浏览器)

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已经成功返回。

原文链接:https://www.cnblogs.com/yhleng/p/6728864.html

ASP.NET HttpWebRequest和HttpWebResponse的更多相关文章

  1. Asp.net HttpWebRequest和HttpWebResponse发送和接受任何类型数据

    发送字符串数据发送数据 string strId = "guest"; "; string postData = "userid=" + strId; ...

  2. C# -- HttpWebRequest 和 HttpWebResponse 的使用 C#编写扫雷游戏 使用IIS调试ASP.NET网站程序 WCF入门教程 ASP.Net Core开发(踩坑)指南 ASP.Net Core Razor+AdminLTE 小试牛刀 webservice创建、部署和调用 .net接收post请求并把数据转为字典格式

    C# -- HttpWebRequest 和 HttpWebResponse 的使用 C# -- HttpWebRequest 和 HttpWebResponse 的使用 结合使用HttpWebReq ...

  3. 利用HttpWebRequest和HttpWebResponse获取Cookie

    之前看过某个同学的一篇有关与使用JSoup解析学校图书馆的文章,仔细一看,发现竟然是同校!!既然对方用的是java,那么我也就来个C#好了,虽然我的入门语言是java. C#没有JSoup这样方便的东 ...

  4. c# HttpWebRequest与HttpWebResponse 绝技(转载)

    c# HttpWebRequest与HttpWebResponse 绝技    如果你想做一些,抓取,或者是自动获取的功能,那么就跟我一起来学习一下Http请求吧.本文章会对Http请求时的Get和P ...

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

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

  6. 利用HttpWebRequest和HttpWebResponse获取Cookie并实现模拟登录

    利用HttpWebRequest和HttpWebResponse获取Cookie并实现模拟登录 tring cookie = response.Headers.Get("Set-Cookie ...

  7. C#使用HttpWebRequest和HttpWebResponse上传文件示例

    C#使用HttpWebRequest和HttpWebResponse上传文件示例 1.HttpHelper类: 复制内容到剪贴板程序代码 using System;using System.Colle ...

  8. c# HttpWebRequest与HttpWebResponse

    [转]c# HttpWebRequest与HttpWebResponse 绝技 如果你想做一些,抓取,或者是自动获取的功能,那么就跟我一起来学习一下Http请求吧. 本文章会对Http请求时的Get和 ...

  9. C# HttpWebRequest与HttpWebResponse详解

    C# HttpWebRequest与HttpWebResponse详解  http://www.codeproject.com/Articles/6554/How-to-use-HttpWebRequ ...

随机推荐

  1. 微信小程序开发中的二三事之网易云信IMSDK DEMO

    本文由作者邹永胜授权网易云社区发布. 简介 为了更好的展示我们即时通讯SDK强悍的能力,网易云信IM SDK微信小程序DEMO的开发就提上了日程.用产品的话说就是: 云信 IM 小程序 SDK 的能力 ...

  2. asp.net mvc全局异常捕获

    通过重写OnException方法形式实现. 1.自定义异常记录类并继承HandleErrorAttribute类. public class HandlerErrorAttribute : Hand ...

  3. 初识阿里开源诊断工具Arthas

    上个月,阿里开源了一个名为Arthas的监控工具.恰逢近期自己在写多线程处理业务,由此想到了一个问题. 如果在本机开发调试,IDE可以看到当前的活动线程,例如IntelliJ IDEA,线程是运行还是 ...

  4. PHP set_error_handler()函数的使用

    我们写程序,难免会有问题(是经常会遇到问题 ),而PHP遇到错误时,就会给出出错脚本的位置.行数和原因.有很多人说,这并没有什么大不了.确实,在调试程序阶段,这确实是没啥的,而且我认为给出错误路径是必 ...

  5. String类的操作方法

    因String属于java核心包lang包的东西,所以不需要导包! /* * 字符串操作 * */ String name = "jck"; String name1 = &quo ...

  6. leecode刷题(21)-- 删除链表的倒数第N个节点

    leecode刷题(21)-- 删除链表的倒数第N个节点 删除链表的倒数第N个节点 描述: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2- ...

  7. leetcode-278-First Bad Version(注意不要上溢)

    题目描述:(说明中有简单翻译) You are a product manager and currently leading a team to develop a new product. Unf ...

  8. float数据在内存中存储方式

    float类型数字在计算机中用4个字节存储.遵循IEEE-754格式标准: 一个浮点数有3部分组成: 符号部分,0 表示正,1表示负. 底数部分 使用二进制数来表示此浮点数的实际值,底数部分实际是占用 ...

  9. centos 7 网站前端中文乱码分析、解决办法

    2019-03-28 1.网站前端中文文字乱码主要原因有两点: (1)mysql数据库内部存储的数据本身处于乱码状态 (2)前端与数据库传输数据的字符集与数据库内部字符集不一致导致 2.查找造成中文乱 ...

  10. 三、OPENERP 中的对象关系类型

    OE中的对象关系一共分四种,one2one,one2many,many2one,many2many.他们的意思分别是一对一,一对多,多对一以及多对多. 我们新建一个模块来测试这四种类型 1.one2o ...