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. }



你可能感兴趣的:(C++,c,Web,C#,mobile)