C# 实现向Web网站Post数据

以下是一段通过ip138站点的请求,以查询手机归属地的代码:

  1.            string postData = "mobile=13824162584&action=mobile";
  2.             byte[] bs = Encoding.ASCII.GetBytes(postData);
  3.             WebRequest req = WebRequest.Create("http://www.ip138.com:8080/search.asp");
  4.             req.Method = "POST";
  5.             req.ContentType = "application/x-www-form-urlencoded";
  6.             req.ContentLength = bs.Length;
  7.            
  8.             using (Stream reqStream = req.GetRequestStream())
  9.             {
  10.                 reqStream.Write(bs, 0, bs.Length);
  11.             }
  12.             
  13.             using (WebResponse wr = req.GetResponse())
  14.             {
  15.                 //在这里对接收到的页面内容进行处理
  16.                 Encoding encode = System.Text.Encoding.GetEncoding("gb2312");
  17.                 string tempStr = "";
  18.                 string sb = "";
  19.                 Stream receviceStream = wr.GetResponseStream();
  20.                 while(true)
  21.                 {
  22.                     byte[] read = new byte[1024];
  23.                     int iBytes = receviceStream.Read(read, 0, read.Length);
  24.                     if (0 == iBytes)
  25.                     {
  26.                         break;
  27.                     }
  28.                     if (iBytes < read.Length)
  29.                     {
  30.                         byte[] buf = new byte[iBytes];
  31.                         for (int i = 0; i < iBytes; i++)
  32.                         {
  33.                             buf[i] = read[i];
  34.                         }
  35.                         tempStr = encode.GetString(buf);
  36.                         sb += tempStr;
  37.                     }
  38.                     else
  39.                     {
  40.                         tempStr = encode.GetString(read);
  41.                         sb += tempStr;
  42.                     }
  43.                 }
  44.             }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值