C#的HttpWebRequest编程,支持带ViewState的网页POST请求
2013-07-04 17:33
771 查看
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, 0, 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; }
View Code
相关文章推荐
- C#的HttpWebRequest编程,支持带ViewState的网页POST请求
- HttpWebRequest支持带ViewState的网页POST请求
- c# HttpWebRequest与HttpWebResponse请求网页和返回网页教程
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- [导入]C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- C# HttpWebRequest提交post请求
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- C#利用HttpWebRequest进行post请求的示例(HTTPS)
- C# 后台使用HttpWebRequest发送POST请求帮助类
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- C# winform端 通过HttpWebRequest进行post和get请求,数据格式为json,后台java端接收,其中有关传输特殊字符(\t,\r,',\n,n)等处理
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- c# HttpWebRequest与HttpWebResponse请求网页和返回网页教程
- C# : WebRequest发起Http Post请求模拟登陆并cookie处理示例
- C#利用HttpWebRequest进行post请求的示例(HTTPS)
- asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- wp7使用POST请求Asp.net网页时,HttpWebRequest的BeginGetResponse方法抛出NotSupportException异常的解决方法
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- c# HttpWebRequest post和get请求http