利用WebApi获取手机号码归属地
前述:
在WebApi中,涉及到一个重要的类,HttpWebRequest。
学习link:httpwebrequest详解
示例演示:
代码示例:
1、前端代码:
1 @{ 2 ViewBag.Title = "Index"; 3 Layout = null; 4 } 5 @*<script src="~/Views/js/jquery-1.7.1.js"></script>*@ 6 @*<script src="~/Views/js/jquery.min.js"></script>*@ 7 <script src="~/Scripts/jquery-1.8.2.min.js"></script> 8 <h2>Index</h2> 9 请输入手机号码:<input type="text" id="inputPhoneNum" value=""/><br /> 10 <input type="button" value="获取归属地" id="getPhoneData"/> 11 <div id="content"></div> 12 <script> 13 $(function () { 14 $('#getPhoneData').click(function () { 15 var str = ""; 16 var inputPhoneNum = $("#inputPhoneNum").val(); 17 $.ajax({ 18 url: "/APITest/GetPhoneAreaDatet", 19 data: { phoneNum: inputPhoneNum }, 20 dataType: "json", 21 success: function (result) { 22 $('#content').empty(); //清空resText里面的所有内容 23 var json = result; 24 var resobj = JSON.parse(json); 25 //{province:'广东',cityname:'广州'} 26 $('#content').html("手机号码" + inputPhoneNum + "的归属地为:" + resobj.province + resobj.cityname); 27 } 28 }); 29 }); 30 }); 31 </script>
2,后台代码:
1 public class APITestController : Controller 2 { 3 // 4 // GET: /APITest/ 5 6 public ActionResult Index() 7 { 8 return View(); 9 } 10 11 public JsonResult GetPhoneAreaDatet() { 12 //1.0 确定url 网络爬虫(蜘蛛) 13 string phoneNum = Request.QueryString["phoneNum"]; 14 string url = "http://virtual.paipai.com/extinfo/GetMobileProductInfo"; 15 string paras = string.Format("mobile={0}&amount={1}&callname={2}" 16 , phoneNum 17 , 10000 18 , "getPhoneNumInfoExtCallback"); 19 string Result = ""; 20 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 21 request.Method = "POST"; 22 //做请求 23 request.ContentType = "application/x-www-form-urlencoded; charset=gb2312"; 24 request.MaximumAutomaticRedirections = 4; 25 request.MaximumResponseHeadersLength = 4; 26 27 byte[] requestBytes = System.Text.Encoding.UTF8.GetBytes(paras); 28 request.ContentLength = requestBytes.Length; 29 30 Stream requestStream = request.GetRequestStream(); 31 requestStream.Write(requestBytes, 0, requestBytes.Length); 32 requestStream.Close(); 33 34 //request.Credentials = CredentialCache.DefaultCredentials; 35 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 36 37 //读结果 38 Stream receiveStream = response.GetResponseStream(); 39 StreamReader readStream = new StreamReader(receiveStream, Encoding.GetEncoding("gb2312")); 40 Result = readStream.ReadToEnd(); 41 response.Close(); 42 readStream.Dispose(); 43 //{mobile:'13926231057',province:'广东',isp:'中国移动',stock:'1',amount:'10000',maxprice:'0',minprice:'0',cityname:'广州'} 44 45 int indexOfProFrist = Result.IndexOf("province")+10; 46 int indexOfProEnd = Result.IndexOf("isp")-2; 47 int indexOfCityFrist = Result.LastIndexOf("cityname")+10; 48 int indexOfCityEnd = Result.IndexOf('}')-1; 49 string strPro = Result.Substring(indexOfProFrist,indexOfProEnd-indexOfProFrist); 50 string strCity = Result.Substring(indexOfCityFrist, indexOfCityEnd - indexOfCityFrist); 51 string strJsObj = "{\"province\":\"" + strPro + "\",\"cityname\":\"" + strCity + "\"}"; 52 //int indexOfName 53 return Json(strJsObj, JsonRequestBehavior.AllowGet); 54 } 55 }
作者:Cboii
本博客所有文章仅用于学习、研究和交流目的,欢迎非商业性质转载。
由于博主的水平不高,不足和错误之处在所难免,希望大家能够批评指出。
在wordpress安装、主题、插件以及开发上面有问题的,可以加入qq群:1140958614(Wp建站每日学习/交流群)进行学习和提问
如果需要建站服务,可以直接联系我的qq:185369045
posted on 2015-06-01 18:23 两宝程序cboii 阅读(3009) 评论(1) 编辑 收藏 举报