C# 实现向Web网站Post数据

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

  1. stringpostData="mobile=13824162584&action=mobile";
  2. byte[]bs=Encoding.ASCII.GetBytes(postData);
  3. WebRequestreq=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. using(StreamreqStream=req.GetRequestStream())
  8. {
  9. reqStream.Write(bs,0,bs.Length);
  10. }
  11. using(WebResponsewr=req.GetResponse())
  12. {
  13. //在这里对接收到的页面内容进行处理
  14. Encodingencode=System.Text.Encoding.GetEncoding("gb2312");
  15. stringtempStr="";
  16. stringsb="";
  17. StreamreceviceStream=wr.GetResponseStream();
  18. while(true)
  19. {
  20. byte[]read=newbyte[1024];
  21. intiBytes=receviceStream.Read(read,0,read.Length);
  22. if(0==iBytes)
  23. {
  24. break;
  25. }
  26. if(iBytes<read.Length)
  27. {
  28. byte[]buf=newbyte[iBytes];
  29. for(inti=0;i<iBytes;i++)
  30. {
  31. buf[i]=read[i];
  32. }
  33. tempStr=encode.GetString(buf);
  34. sb+=tempStr;
  35. }
  36. else
  37. {
  38. tempStr=encode.GetString(read);
  39. sb+=tempStr;
  40. }
  41. }
  42. }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值