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