利用WebApi擷取手機號碼歸屬地

來源:互聯網
上載者:User

標籤:

前述:

  在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                , phoneNum17                , 1000018                , "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 indexOfName53             return Json(strJsObj, JsonRequestBehavior.AllowGet);54         }55     }

 

利用WebApi擷取手機號碼歸屬地

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.