C#的HttpWebRequest编程,支持带ViewState的网页POST请求
staticprivatestring SearchURL(string id)
{
try
{
//Get the ViewState and EventValidation
HttpWebRequest request = WebRequest.Create(URI) as HttpWebRequest;
request.Method ="GET";
request.KeepAlive =false; //Get the response
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
System.IO.Stream responseStream = response.GetResponseStream();
System.IO.StreamReader reader =new System.IO.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 ViewState
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);
eventValidation = Uri.EscapeDataString(eventValidation);
string strSearch = Uri.EscapeDataString("查询"); string formatString ="__VIEWSTATE={0}&__EVENTVALIDATION={1}&TextBox1={2}&Button1={3}";
string postString =string.Format(formatString, viewState, eventValidation, id,strSearch); //Change to byte[]
byte[] postData = Encoding.ASCII.GetBytes(postString); //Compose the new request
request = WebRequest.Create(URI) as HttpWebRequest;
request.Method ="POST";
request.KeepAlive =false;
request.ContentType ="application/x-www-form-urlencoded";
request.ContentLength = postData.Length; System.IO.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 System.IO.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");
}
returnnull;
}
C#的HttpWebRequest编程,支持带ViewState的网页POST请求的更多相关文章
- HttpwebRequest - 带ViewState的网页POST请求
这是我今天下午碰到的案例,一个退订页面的post请求,请求头信息都很明确,but看看下面这个请求体,除了最后一个key是我的页面控件名称,其他的几个ViewState相关都是what呢?(ViewSt ...
- 使用PHP Socket 编程模拟Http post和get请求
这里给大家分享一段使用PHP Socket 编程模拟Http post和get请求的代码,非常的实用,结尾部分我们再讨论下php模拟http请求的几种方法. <?php /** * 使用PHP ...
- HttpWebRequest 改为 HttpClient 踩坑记-请求头设置
HttpWebRequest 改为 HttpClient 踩坑记-请求头设置 Intro 这两天改了一个项目,原来的项目是.net framework 项目,里面处理 HTTP 请求使用的是 WebR ...
- WiFi-ESP8266入门http(3-4)网页一键配网(1若为普通wifi直连 2若为西电网页认证自动网页post请求连接)+网页按钮灯控+MQTT通信
网页一键配网(1若为普通wifi直连 2若为西电网页认证自动网页post请求连接)+网页按钮灯控+MQTT通信 工程连接:https://github.com/Dongvdong/ESP8266_H ...
- 使用HttpWebRequest和HtmlAgilityPack抓取网页(拒绝乱码,拒绝正则表达式)
废话不多说, 直接说需求. 公司的网站需要抓取其他网站的文章,但任务没到我这,同事搞了一下午没搞出来.由于刚刚到公司, 想证明下自己,就把活揽过来了.因为以前做过,觉得应该很简单,但当我开始做的时候, ...
- [转]Linux Socket编程 Socket抓取网页源码
“一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. ——有感于实际编程和开源项目研究. 我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览 ...
- [Python网络编程]gevent httpclient以及网页编码
之前看到geventhttpclient这个项目,https://github.com/gwik/geventhttpclient,官方文档说非常快,因为响应使用了C的解析,所以我一直想把这玩意用到项 ...
- Python -- 网络编程 -- 简单抓取网页
抓取网页: urllib.request.urlopen(url).read().decode('utf-8') --- (百度是utf-8,谷歌不是utf-8,也不是cp936,ascii也不行 ...
- Powerbuilder编程技巧 如何获取网页的HTML源码
直接使用的三种方式 1. PB内部对象 Inet object 2. API 函数 3. Ole中的Microsfot Web 游览器对象 一.Inet object: 1.Inet objec ...
随机推荐
- 统计svn 代码提交情况
统计svn代码提交,使用工具 statsvn.jar 下载地址:http://sourceforge.net/projects/statsvn/ rem 声明一个时间变量 作为文件名 %time:~, ...
- JEECG图表配置说明
图表配置可以做什么? 图表配置可以通过在线配置,无需编写代码生成图形报表页面.使用highcharts.js实现,可以运行在任何现代浏览器,包括移动终端以及IE6.目前支持曲线图.柱状图等基础报表. ...
- ISE在win8.1的安装问题
问题1:.lic无法打开 打开:C:\Xilinx\14.6\ISE_DS\ISE\lib\nt64 思路是这样: 将libPortability.dll重命名(加尾缀.orig,意思是origina ...
- 基于PHP规范的自动加载方式(composer配置)
针对PHP这种编程语言,到目前FIG指定了五个规范,分别如下: PSR0:自动加载: PSR1:基本代码规范: PSR2:代码样式规范: PSR3:日志接口规范: PSR4:自动加载规范: 看上去PS ...
- 深入讲解Android Property机制
深入讲解Android Property机制 侯亮 1 概述 Android系统(本文以Android 4.4为准)的属性(Property)机制有点儿类似Windows系统的注册表,其中的 ...
- intercepting IO request
Introduction to File System Filters https://msdn.microsoft.com/en-us/library/windows/hardware/dn6416 ...
- 每日英语:Secrets Of Effective Office Humor
Margot Carmichael Lester loves making good-natured jokes at work. As owner of The Word Factory, a Ca ...
- LeetCode: Trapping Rain Water 解题报告
https://oj.leetcode.com/problems/trapping-rain-water/ Trapping Rain WaterGiven n non-negative intege ...
- js中引号("")中间设置变量
- Django 缓存、信号
Reference: http://www.cnblogs.com/lianzhilei/p/6365877.html 缓存 由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访 ...