利用WebApi获取手机号码归属地
2015-06-01 18:23
471 查看
前述:
在WebApi中,涉及到一个重要的类,HttpWebRequest。
学习link:httpwebrequest详解
示例演示:
代码示例:
1、前端代码:
2,后台代码:
在WebApi中,涉及到一个重要的类,HttpWebRequest。
学习link:httpwebrequest详解
示例演示:
代码示例:
1、前端代码:
@{ ViewBag.Title = "Index"; Layout = null; } @*<script src="~/Views/js/jquery-1.7.1.js"></script>*@ @*<script src="~/Views/js/jquery.min.js"></script>*@ <script src="~/Scripts/jquery-1.8.2.min.js"></script> <h2>Index</h2> 请输入手机号码:<input type="text" id="inputPhoneNum" value=""/><br /> <input type="button" value="获取归属地" id="getPhoneData"/> <div id="content"></div> <script> $(function () { $('#getPhoneData').click(function () { var str = ""; var inputPhoneNum = $("#inputPhoneNum").val(); $.ajax({ url: "/APITest/GetPhoneAreaDatet", data: { phoneNum: inputPhoneNum }, dataType: "json", success: function (result) { $('#content').empty(); //清空resText里面的所有内容 var json = result; var resobj = JSON.parse(json); //{province:'广东',cityname:'广州'} $('#content').html("手机号码" + inputPhoneNum + "的归属地为:" + resobj.province + resobj.cityname); } }); }); }); </script>
2,后台代码:
public class APITestController : Controller { // // GET: /APITest/ public ActionResult Index() { return View(); } public JsonResult GetPhoneAreaDatet() { //1.0 确定url 网络爬虫(蜘蛛) string phoneNum = Request.QueryString["phoneNum"]; string url = "http://virtual.paipai.com/extinfo/GetMobileProductInfo"; string paras = string.Format("mobile={0}&amount={1}&callname={2}" , phoneNum , 10000 , "getPhoneNumInfoExtCallback"); string Result = ""; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; //做请求 request.ContentType = "application/x-www-form-urlencoded; charset=gb2312"; request.MaximumAutomaticRedirections = 4; request.MaximumResponseHeadersLength = 4; byte[] requestBytes = System.Text.Encoding.UTF8.GetBytes(paras); request.ContentLength = requestBytes.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(requestBytes, 0, requestBytes.Length); requestStream.Close(); //request.Credentials = CredentialCache.DefaultCredentials; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //读结果 Stream receiveStream = response.GetResponseStream(); StreamReader readStream = new StreamReader(receiveStream, Encoding.GetEncoding("gb2312")); Result = readStream.ReadToEnd(); response.Close(); readStream.Dispose(); //{mobile:'13926231057',province:'广东',isp:'中国移动',stock:'1',amount:'10000',maxprice:'0',minprice:'0',cityname:'广州'} int indexOfProFrist = Result.IndexOf("province")+10; int indexOfProEnd = Result.IndexOf("isp")-2; int indexOfCityFrist = Result.LastIndexOf("cityname")+10; int indexOfCityEnd = Result.IndexOf('}')-1; string strPro = Result.Substring(indexOfProFrist,indexOfProEnd-indexOfProFrist); string strCity = Result.Substring(indexOfCityFrist, indexOfCityEnd - indexOfCityFrist); string strJsObj = "{\"province\":\"" + strPro + "\",\"cityname\":\"" + strCity + "\"}"; //int indexOfName return Json(strJsObj, JsonRequestBehavior.AllowGet); } }
相关文章推荐
- JavaScript HTML DOM
- 黑马程序员——IO流(三)
- Android学习 - Android系统的智能指针(轻量级指针、强指针和弱指针)的实现原理分析
- ORA-28001: the password has expired解决方法
- IIS7本地ASP(错误 80004005)解决方法
- C++ assert 的一点说明
- Python中写一个乒乓球类的游戏
- java中栈的简单操作
- 线程函数的设计以及MsgWaitForMultipleObjects函数的使用要点
- Jquery autocomplete插件的使用
- AFNetworking实现程序重新启动时的断点续传
- ArcGIS Add-In调试无法重新生成
- @SuppressWarnings
- ny-单调递增最长子序列
- IUSR和IIS_IUSRS
- 提取 Microsoft.ReportViewer等dll
- iOS中Objective-C与JavaScript之间相互调用的实现(实现了与Android相同的机制)
- HDU 2896 病毒侵袭(AC自动机)
- AsyncTask实现ListView中异步加载网络图片
- Spring学习笔记——关于Spring注解扫描不能注入new对象问题