这是我今天下午碰到的案例,一个退订页面的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请求的更多相关文章

  1. C#的HttpWebRequest编程,支持带ViewState的网页POST请求

    staticprivatestring SearchURL(string id) { try { //Get the ViewState and EventValidation HttpWebRequ ...

  2. 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求

    通用辅助类  下面是我编写的一个辅助类,在这个类中采用了HttpWebRequest中发送GET/HTTP/HTTPS请求,因为有的时候需 要获取认证信息(如Cookie),所以返回的是HttpWeb ...

  3. (转) 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求

    转自:http://blog.csdn.net/zhoufoxcn/article/details/6404236 通用辅助类 下面是我编写的一个辅助类,在这个类中采用了HttpWebRequest中 ...

  4. 【转】在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求

    http://zhoufoxcn.blog.51cto.com/792419/561934 这个需求来自于我最近练手的一个项目,在项目中我需要将一些自己发表的和收藏整理的网文集中到一个地方存放,如果全 ...

  5. 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求【转载】

    标签:C# HTTPS HttpWebRequest HTTP HttpWebResponse 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任. ...

  6. C#中用HttpWebRequest中发送GET/HTTP/HTTPS请求 (转载)

    这个需求来自于我最近练手的一个项目,在项目中我需要将一些自己发表的和收藏整理的网文集中到一个地方存放,如果全部采用手工操作工作量大而且繁琐,因此周公决定利用C#来实现.在很多地方都需要验证用户身份才可 ...

  7. 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求(转)

    通用辅助类 下面是我编写的一个辅助类,在这个类中采用了HttpWebRequest中发送GET/HTTP/HTTPS请求,因为有的时候需要获取认证信息(如Cookie),所以返回的是HttpWebRe ...

  8. C#中用HttpWebRequest中发送GET/HTTP/HTTPS请求

    C# HttpWebRequest GET HTTP HTTPS 请求  作者:周公(zhoufoxcn)    原文:http://blog.csdn.net/zhoufoxcn 这个需求来自于我最 ...

  9. 一篇文章带你了解网页框架——Vue简单入门

    一篇文章带你了解网页框架--Vue简单入门 这篇文章将会介绍我们前端入门级别的框架--Vue的简单使用 如果你以后想从事后端程序员,又想要稍微了解前端框架知识,那么这篇文章或许可以给你带来帮助 温馨提 ...

随机推荐

  1. CentOS安装中文支持包

    修改配置文件 LANG="zh_CN.UTF-8" 改为中文字符集 然后在查看更改后的系统语言变量 [root@5c46832b5c01 ~]# locale locale: Ca ...

  2. PAT甲级1139 First Contact

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805344776077312 题意: 有m对朋友关系,每个人用4为数 ...

  3. AtCoder Beginner Contest 070|Elena|8.12|#471

    打了场beginner的AtCoder,也是我第一次打AtCoder,虽然AK了,但是由于手速慢+撒比,才#471… 比赛链接:https://beta.atcoder.jp/contests/abc ...

  4. MS14-064 漏洞测试入侵——20145301

    MS14-064 漏洞测试入侵 Microsoft Windows OLE远程代码执行漏洞,OLE(对象链接与嵌入)是一种允许应用程序共享数据和功能的技术 执行摘要 此安全更新可解决 Microsof ...

  5. pytorch的torch.utils.data.DataLoader认识

    PyTorch中数据读取的一个重要接口是torch.utils.data.DataLoader,该接口定义在dataloader.py脚本中,只要是用PyTorch来训练模型基本都会用到该接口, 该接 ...

  6. 1.7Oob成员变量和局部变量疑难区分

    import java.util.Scanner; public class booleann { private float fWidth; private float fHeight; void ...

  7. MySQL的一些概念

    数据库与服务器.客户端的层次关系 关于数据库 程序中需要存储数据的方式: 1 变量(列表.元组.集合.字典.嵌套) 2 外存(文件)(*.ini) 3 表格.Excel(*.xls.*.xlsx.*. ...

  8. page 页 分页 分段

    小结: 1. 页:磁盘和内存间传输数据的最小单位: MySQL: What is a page? https://stackoverflow.com/questions/4401910/mysql-w ...

  9. Server:www121 Server:www120 Server:NWS_SP

    Request URL:http://www.biyao.com/minisite/bzzx Request Method:GET Status Code:200 OK Remote Address: ...

  10. PHP之错误

    三.PHP配置之Error handling logging 1.error_reporting integer error_reporting = E_ALL 设置错误报告的级别.该参数可以是一个任 ...