以下是一段通过ip138站点的请求,以查询手机归属地的代码:
- string postData = "mobile=13824162584&action=mobile";
- byte[] bs = Encoding.ASCII.GetBytes(postData);
- WebRequest req = WebRequest.Create("http://www.ip138.com:8080/search.asp");
- req.Method = "POST";
- req.ContentType = "application/x-www-form-urlencoded";
- req.ContentLength = bs.Length;
- using (Stream reqStream = req.GetRequestStream())
- {
- reqStream.Write(bs, 0, bs.Length);
- }
- using (WebResponse wr = req.GetResponse())
- {
- //在这里对接收到的页面内容进行处理
- Encoding encode = System.Text.Encoding.GetEncoding("gb2312");
- string tempStr = "";
- string sb = "";
- Stream receviceStream = wr.GetResponseStream();
- while(true)
- {
- byte[] read = new byte[1024];
- int iBytes = receviceStream.Read(read, 0, read.Length);
- if (0 == iBytes)
- {
- break;
- }
- if (iBytes < read.Length)
- {
- byte[] buf = new byte[iBytes];
- for (int i = 0; i < iBytes; i++)
- {
- buf[i] = read[i];
- }
- tempStr = encode.GetString(buf);
- sb += tempStr;
- }
- else
- {
- tempStr = encode.GetString(read);
- sb += tempStr;
- }
- }
- }