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的简单使用 如果你以后想从事后端程序员,又想要稍微了解前端框架知识,那么这篇文章或许可以给你带来帮助 温馨提 ...
随机推荐
- 180425、cookie工具类
package com.thinkgem.jeesite.common.utils; import java.io.UnsupportedEncodingException; import java. ...
- DB2 Version 10.5 补丁下载
DB2 Version 10.5 for Linux, UNIX, and Windows fix pack summary https://www.ibm.com/support/knowledge ...
- Steeltoe之Service Discovery篇
在前文一窥Spring Cloud Eureka中,已经构建了基于Eureka的服务端与客户端,可用于实现服务注册与发现功能.而借助Steeltoe的类库,可以在.NET生态系统中使用Spring C ...
- Elasticsearch全文检索实战小结
一.项目概述 这是一个被我称之为“没有枪.没有炮,硬着头皮自己造”的项目.项目是和其它公司合作的三个核心模块开发. 使用ES的目的是: 1).采集数据.网站数据清洗后存入ES: 2).对外提供精确检索 ...
- hbase本地模式-安装及基本测试
解压缩hbase二进制安装文件到/opt目录下: #tar -zxvf hbase-0.98.6-cdh5.3.6.tar.gz -C /opt/cdh-5.3.6/ 编辑配置文件,这里仅配置数据目录 ...
- HDMI 接口及CEC信号
HDMI 接口及CEC信号 2016年12月02日 14:16:38 King-Five 阅读数:16389 HDMI接口 HDMI(High Definition Multimedia Interf ...
- azkaban---visualize crontab--frontail
azkaban---visualize crontab azkaban--docker-----http://www.jkeabc.com/254015.html azkaban--tips ht ...
- Page9:结构分解以及系统内部稳定和BIBO稳定概念及其性质[Linear System Theory]
内容包含系统能控性结构分解.系统能观测性结构分解以及系统结构规范分解原理,线性系统的内部稳定.BIBO稳定概念及其性质
- [skill] mmap / fwrite / write linux磁盘读写的分层结构
转自:http://www.cnblogs.com/zhaoyl/p/5901680.html 看完此文,题目不言自明.转自 http://blog.chinaunix.net/uid-2710571 ...
- 转:JDBC中关于PreparedStatement.setObject的一些细节说明
原文地址:https://blog.csdn.net/zhiyangxuzs/article/details/78657235 JDBC中PreparedStatement.setObject(ind ...