HttpwebRequest - 带ViewState的网页POST请求
这是我今天下午碰到的案例,一个退订页面的post请求,请求头信息都很明确,but看看下面这个请求体,除了最后一个key是我的页面控件名称,其他的几个ViewState相关都是what呢?(ViewState原理参考http://www.cnblogs.com/wwan/archive/2010/11/18/1880357.html),下面是怎么处理这种情况的Post请求的办法。
1. 拿到接口地址,参数,拼串发送Get请求,拿到响应体(如下图,看高亮)
2. 从响应体里提取我们想要的ViewState相关,并保持在相应的字符串里面
3. 拼出来我们想要的请求体,对刚才的地址重新发送Post请求

亲测,可以解决问题。
请求头 Content-Type:Content-Type: application/x-www-form-urlencoded

public static string unsubResponse(string UnsubURL)
{
try
{
// Get the ViewState and EventValidation
HttpWebRequest request = WebRequest.Create(UnsubURL) as HttpWebRequest;
request.Method = "GET";
request.KeepAlive = false; // Get the response
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
string srcString = reader.ReadToEnd(); //Get the __VIEWSTATE
string viewStateFlag = "id=\"__VIEWSTATE\" value=\"";
int i = srcString.IndexOf(viewStateFlag) + viewStateFlag.Length;
int j = srcString.IndexOf("\"", i);
string viewState = srcString.Substring(i, j - i); // Get the __VIEWSTATEGENERATOR
string viewStateGeneratorFlag = "id=\"__VIEWSTATEGENERATOR\" value=\"";
i = srcString.IndexOf(viewStateGeneratorFlag) + viewStateGeneratorFlag.Length;
j = srcString.IndexOf("\"", i);
string viewStateGenerator = srcString.Substring(i, j - i); // Get the __EVENTVALIDATION
string EventValidationFlag = "id=\"__EVENTVALIDATION\" value=\"";
i = srcString.IndexOf(EventValidationFlag) + EventValidationFlag.Length;
j = srcString.IndexOf("\"", i);
string eventValidation = srcString.Substring(i, j - i); // Compose the URL
viewState = Uri.EscapeDataString(viewState);
viewStateGenerator = Uri.EscapeDataString(viewStateGenerator);
eventValidation = Uri.EscapeDataString(eventValidation);
string unsubButton = Uri.EscapeDataString("Unsubscribe"); string formatString = "__VIEWSTATE={0}&__VIEWSTATEGENERATOR={1}&__EVENTVALIDATION={2}&ButtonUnsubscribe={3}";
string postString = string.Format(formatString, viewState, viewStateGenerator, eventValidation, unsubButton); // Change to byte[]
byte[] postData = Encoding.ASCII.GetBytes(postString); // Compose the new request
request = WebRequest.Create(UnsubURL) as HttpWebRequest;
request.Method = "POST";
request.KeepAlive = false;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length; Stream outputStream = request.GetRequestStream();
outputStream.Write(postData, , postData.Length);
outputStream.Close(); // Get the new response
response = request.GetResponse() as HttpWebResponse;
responseStream = response.GetResponseStream();
reader = new StreamReader(responseStream);
srcString = reader.ReadToEnd();
return (srcString);
}
catch (WebException we)
{
Console.WriteLine("Communication error," + we.Message + " please check your connectivity and try again.", "Error");
}
catch
{
Console.WriteLine("Unknow error.", "Error");
}
return (null);
}
HttpwebRequest - 带ViewState的网页POST请求的更多相关文章
- C#的HttpWebRequest编程,支持带ViewState的网页POST请求
staticprivatestring SearchURL(string id) { try { //Get the ViewState and EventValidation HttpWebRequ ...
- 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求
通用辅助类 下面是我编写的一个辅助类,在这个类中采用了HttpWebRequest中发送GET/HTTP/HTTPS请求,因为有的时候需 要获取认证信息(如Cookie),所以返回的是HttpWeb ...
- (转) 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求
转自:http://blog.csdn.net/zhoufoxcn/article/details/6404236 通用辅助类 下面是我编写的一个辅助类,在这个类中采用了HttpWebRequest中 ...
- 【转】在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求
http://zhoufoxcn.blog.51cto.com/792419/561934 这个需求来自于我最近练手的一个项目,在项目中我需要将一些自己发表的和收藏整理的网文集中到一个地方存放,如果全 ...
- 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求【转载】
标签:C# HTTPS HttpWebRequest HTTP HttpWebResponse 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任. ...
- C#中用HttpWebRequest中发送GET/HTTP/HTTPS请求 (转载)
这个需求来自于我最近练手的一个项目,在项目中我需要将一些自己发表的和收藏整理的网文集中到一个地方存放,如果全部采用手工操作工作量大而且繁琐,因此周公决定利用C#来实现.在很多地方都需要验证用户身份才可 ...
- 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求(转)
通用辅助类 下面是我编写的一个辅助类,在这个类中采用了HttpWebRequest中发送GET/HTTP/HTTPS请求,因为有的时候需要获取认证信息(如Cookie),所以返回的是HttpWebRe ...
- C#中用HttpWebRequest中发送GET/HTTP/HTTPS请求
C# HttpWebRequest GET HTTP HTTPS 请求 作者:周公(zhoufoxcn) 原文:http://blog.csdn.net/zhoufoxcn 这个需求来自于我最 ...
- 一篇文章带你了解网页框架——Vue简单入门
一篇文章带你了解网页框架--Vue简单入门 这篇文章将会介绍我们前端入门级别的框架--Vue的简单使用 如果你以后想从事后端程序员,又想要稍微了解前端框架知识,那么这篇文章或许可以给你带来帮助 温馨提 ...
随机推荐
- go 的文件处理
准备一个文件 imooc.txt hello world! 一.使用 io/ioutil 包 定义一个 check 函数 func check(err error) { if err != nil { ...
- np.mgird np.ogrid
np.ogrid: address:https://docs.scipy.org/doc/numpy/reference/generated/numpy.ogrid.html returns an o ...
- Steeltoe之Config客户端篇
Steeltoe是一款开源项目,其目标是选取源自Netflix及其它公司的工具,使它们能够运用于.NET社区.它不仅可以在.NET Core上,也可以在.NET Framework 4.X以上使用.此 ...
- NLP任务:给定一句话,找出这句话中你想要的关键词,包括起始结束索引
在实际的nlp实际任务中,你有一大堆的人工标注的关键词,来新的一句话,找出这句话中的关键词,以便你以后使用,那如何来做呢? 1)用到正则的 finditer()方法,返回你匹配的关键词的迭代对象,包含 ...
- python中的os.path.dirname(__file__)的使用
在编程时,我们要获取当前文件所在的路径,以适合所有的工程,建立相对路径. python的os.path.dirname(__file__)非常好用,建议大家使用: import os FILE = o ...
- 安装ReactNative开发IDE
https://blog.csdn.net/u014484863/article/details/51554428 https://github.com/reactnativecn/react-nat ...
- php值callback类型和匿名函数(闭包)
callback.callable类型 自PHP5.4起可以使用callable类型制定回调类型callback. 本文档基于同样理由使用callback类型信息. 一些函数如call_user_fu ...
- java string和int之间的相互转化
java 中string和int之间的相互转化 1 如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); ...
- 2018/03/08 每日一个Linux命令 之 chattr/lsattr
每日一个Linux命令 2018-03-08 Linux 命令 chattr/lsattr chattr [-参数] [+/-属性] [文件或者目录] 经过今天没有对铃,粥熬糊了,我就知道...... ...
- 启动虚拟机提示"Units specified don’t exist SHSUCDX can’t install"
新建虚拟机快速分区后启动报"Units specified don’t exist SHSUCDX can’t install",试过网上说的 修改BIOS设置方法不起作用 修改虚 ...