特色栏目

ASP源码

PHP源码

.NET源码

JSP源码

游戏频道
专题合集
关闭菜单
首页> ASP.NET教程> 利用WebRequest来实现模拟浏览器通过Post方式向服务器提交数据

利用WebRequest来实现模拟浏览器通过Post方式向服务器提交数据

时间:2009-06-30 16:14:07 作者:互联网

//通过Post发送的数据
   string payload="chkbook=book&keyword=管理";
   WebRequest req = We***quest.Create("http://localhost/pceo/Search.aspx");
   re***ethod = "POST";
   re***ontentType = "application/x-www-form-urlencoded";
   StringBuilder UrlEncoded = new StringBuilder();
   Char[] reserved = {'?', '=', '&'};
   byte[] SomeBytes = null;
   if (payload != null)
   {
    int i=0, j;
    while(i<pa***ad.Length)
    {
     j=***load.IndexOfAny(reserved, i);
     if (j==-1)
     {
      Ur***coded.Append(Ht***tility.UrlEncode(pa***ad.Substring(i, pa***ad.Length-i),Sy***m.Text .Encoding .GetEncoding ("gb2312")));
      break;
     }
     Ur***coded.Append(Ht***tility.UrlEncode(pa***ad.Substring(i, j-i),Sy***m.Text .Encoding .GetEncoding ("gb2312")));
     Ur***coded.Append(pa***ad.Substring(j,1));
     i = j+1;
    }
    SomeBytes = En***ing.Default.GetBytes(Ur***coded.ToString());
    re***ontentLength = So***ytes.Length;
    Stream newStream = re***etRequestStream();
    ne***ream.Write(SomeBytes, 0, So***ytes.Length);
    ne***ream.Close();
   }
   else
   {
    re***ontentLength = 0;
   }
   try
   {
    WebResponse result = re***etResponse();
    Stream ReceiveStream = re***t.GetResponseStream();

    Byte[] read = new Byte[512];
    int bytes = Re***veStream.Read(read, 0, 512);

    tx***ML.InnerHtml = "";
    while (bytes > 0)
    {

     // 注意:
     // 下面假定响应使用 UTF-8 作为编码方式。
     // 如果内容以 ANSI 代码页形式(例如,932)发送,则使用类似下面的语句:
     //  Encoding encode = Sy***m.Text.Encoding.GetEncoding("shift-jis");
     Encoding encode = Sy***m.Text.Encoding.GetEncoding("gb2312");
     tx***ML.InnerHtml = tx***ML.InnerHtml + en***e.GetString(read, 0, bytes);
     bytes = Re***veStream.Read(read, 0, 512);
    }
   }
   catch(Exception)
   {
    tx***ML.InnerHtml = "检索页时出错";
   }

出处:狂风之家 BLOG

相关文章 最新文章

相关应用

热门文章

猜你喜欢

返回顶部