zoukankan      html  css  js  c++  java
  • 利用WebRequest来实现模拟浏览器通过Post方式向服务器提交数据

       //通过Post发送的数据
       string payload=”chkbook=book&keyword=管理”;
       WebRequest req = WebRequest.Create(”http://localhost/pceo/Search.aspx”);
       req.Method = “POST”;
       req.ContentType = “application/x-www-form-urlencoded”;
       StringBuilder UrlEncoded = new StringBuilder();
       Char[] reserved = {’?', ‘=’, ‘&’};
       byte[] SomeBytes = null;
       if (payload != null)
       {
       int i=0, j;
       while(i<payload.Length)
       {
       j=payload.IndexOfAny(reserved, i);
       if (j==-1)
       {
       UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, payload.Length-i),System.Text .Encoding .GetEncoding (”gb2312″)));
       break;
       }
       UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, j-i),System.Text .Encoding .GetEncoding (”gb2312″)));
       UrlEncoded.Append(payload.Substring(j,1));
       i = j+1;
       }
       SomeBytes = Encoding.Default.GetBytes(UrlEncoded.ToString());
       req.ContentLength = SomeBytes.Length;
       Stream newStream = req.GetRequestStream();
       newStream.Write(SomeBytes, 0, SomeBytes.Length);
       newStream.Close();
       }
       else
       {
       req.ContentLength = 0;
       }
       try
       {
       WebResponse result = req.GetResponse();
       Stream ReceiveStream = result.GetResponseStream();
      
       Byte[] read = new Byte[512];
       int bytes = ReceiveStream.Read(read, 0, 512);
      
       txtHTML.InnerHtml = “”;
       while (bytes > 0)
       {
      
       // 注意:
       // 下面假定响应使用 UTF-8 作为编码方式。
       // 如果内容以 ANSI 代码页形式(例如,932)发送,则使用类似下面的语句:
       // Encoding encode = System.Text.Encoding.GetEncoding(”shift-jis”);
       Encoding encode = System.Text.Encoding.GetEncoding(”gb2312″);
       txtHTML.InnerHtml = txtHTML.InnerHtml + encode.GetString(read, 0, bytes);
       bytes = ReceiveStream.Read(read, 0, 512);
       }
       }
       catch(Exception)
       {
       txtHTML.InnerHtml = “检索页时出错”;
       }

  • 相关阅读:
    计算机漏洞安全相关的概念POC 、EXP 、VUL 、CVE 、0DAY
    开始使用kali的一些小问题:菜鸟瞎折腾
    nmap参数详解(罗列一下)
    安装kali之后必做的几件小事
    Debian下virtualBox增强功能出错
    ArcGIS Engine 基础功能(一)
    sublime 配置简单的python环境
    解决 ‘Could not fetch URL https://pypi.python.org’的问题
    golang基础语法学习
    大象盒子技术栈
  • 原文地址:https://www.cnblogs.com/linzheng/p/1872231.html
Copyright © 2011-2022 走看看