ASP.NET HttpWebRequest和HttpWebResponse
HttpWebRequest和HttpWebResponse类是用于发送和接收HTTP数据的最好选择。它们支持一系列有用的属性。
模拟艺龙旅游网登录
想模拟登录,首先整理一下流程
1.通过360浏览器(IE,火狐等等)F12开发人员工具抓到相关数据
2.获取验证码(拿到cookie),登录时也需要使用
3.登录
-------------------------------
F12调出开发人员工具,输入用户名,密码登录,看我们抓到了什么信息。 (此处为360浏览器)

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的更多相关文章
- Asp.net HttpWebRequest和HttpWebResponse发送和接受任何类型数据
发送字符串数据发送数据 string strId = "guest"; "; string postData = "userid=" + strId; ...
- 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 ...
- 利用HttpWebRequest和HttpWebResponse获取Cookie
之前看过某个同学的一篇有关与使用JSoup解析学校图书馆的文章,仔细一看,发现竟然是同校!!既然对方用的是java,那么我也就来个C#好了,虽然我的入门语言是java. C#没有JSoup这样方便的东 ...
- c# HttpWebRequest与HttpWebResponse 绝技(转载)
c# HttpWebRequest与HttpWebResponse 绝技 如果你想做一些,抓取,或者是自动获取的功能,那么就跟我一起来学习一下Http请求吧.本文章会对Http请求时的Get和P ...
- C# 利用 HttpWebRequest 和 HttpWebResponse 模拟登录有验证码的网站
原文:C# 利用 HttpWebRequest 和 HttpWebResponse 模拟登录有验证码的网站 我们经常会碰到需要程序模拟登录一个网站,那如果网站需要填写验证码的要怎样模拟登录呢?这篇文章 ...
- 利用HttpWebRequest和HttpWebResponse获取Cookie并实现模拟登录
利用HttpWebRequest和HttpWebResponse获取Cookie并实现模拟登录 tring cookie = response.Headers.Get("Set-Cookie ...
- C#使用HttpWebRequest和HttpWebResponse上传文件示例
C#使用HttpWebRequest和HttpWebResponse上传文件示例 1.HttpHelper类: 复制内容到剪贴板程序代码 using System;using System.Colle ...
- c# HttpWebRequest与HttpWebResponse
[转]c# HttpWebRequest与HttpWebResponse 绝技 如果你想做一些,抓取,或者是自动获取的功能,那么就跟我一起来学习一下Http请求吧. 本文章会对Http请求时的Get和 ...
- C# HttpWebRequest与HttpWebResponse详解
C# HttpWebRequest与HttpWebResponse详解 http://www.codeproject.com/Articles/6554/How-to-use-HttpWebRequ ...
随机推荐
- Android 色差(尤其白色)的解决办法
Android 中有时出现色差,我碰到的情况是 Galaxy ACE4 中的白色和系统白色不同,所以显示时候颜色不同,很难看. 我发现的问题是 Color.white, android.R.color ...
- MySQL之LIMIT用法
http://blog.163.com/niuxiangshan@126/blog/static/17059659520101081058299/ 看的人家的 mysql支持limitselect * ...
- ElasticSearch基本查询
词条查询 这是一个简单查询.它仅 匹配给定字段中包含该词条的稳定,且是2未经分析的确切的词条. { “query” :{ “term”:{ “title”:”crime” } } } 多词条查询 匹配 ...
- [ActionScript3.0] 使用FileReference处理单个文件的上载
package { import flash.display.SimpleButton; import flash.display.Sprite; import flash.errors.Illega ...
- [转] LVM分区在线扩容
[转] LVM分区在线扩容 在线扩容的这台服务器,LV分区格式为xfs,原大小1.2TB.增加了一块硬盘,大小为1.8TB. fdisk /dev/cciss/c0d1 # 创建分区,并指定分区类型为 ...
- day00 预习 ------基础数据类型预习 ,int ,str ,bool ,dict ,set ,切片,等相关
知识点明确 1 int 2 str 3 元祖 4.列表 5. 字典 6 集合 7 布尔 1 int 数据类型 int 数据类型指的是. 数字型的内容 ,主要用于计算, 2 str 字符类型 str ...
- 【转载】Java 9 新特性——模块化
来自 <http://www.jianshu.com/p/053a5ca89bbb#> 前言 年,我们将迎来 Java 语言的 22 岁生日,22岁,对于一个人而言,正是开始大展鸿图的年纪 ...
- k-近邻算法 python实现
必要的注释已经写在code里面了: import operator from numpy import* def init(): grp=array([[1.0,1.1],[1.0,1.0],[0,0 ...
- python3.6.4没有raw_input
之前是一直在用Python2.7版本,2.7里面raw_input()和input个人认为区别在于raw_input()可以输入字符串和中文,而input()只接受数字,输入字符串就会报错. 现在用的 ...
- Mac下Homebrew的图形化界面工具Cakebrew
安装: brew cask install cakebrew 如果不能下载直接上官网下载dmg包进行安装. 参考: https://www.cakebrew.com/ https://github.c ...