利用WebApi获取手机号码归属地

前述:

  在WebApi中,涉及到一个重要的类,HttpWebRequest。

  学习link:httpwebrequest详解

示例演示:

利用WebApi获取手机号码归属地

代码示例:

  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
,
, "getPhoneNumInfoExtCallback");
string Result = "";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
//做请求
request.ContentType = "application/x-www-form-urlencoded; charset=gb2312";
request.MaximumAutomaticRedirections = ;
request.MaximumResponseHeadersLength = ; byte[] requestBytes = System.Text.Encoding.UTF8.GetBytes(paras);
request.ContentLength = requestBytes.Length; Stream requestStream = request.GetRequestStream();
requestStream.Write(requestBytes, , 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")+;
int indexOfProEnd = Result.IndexOf("isp")-;
int indexOfCityFrist = Result.LastIndexOf("cityname")+;
int indexOfCityEnd = Result.IndexOf('}')-;
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);
}
}
上一篇:SNF开发平台WinForm之二-开发-单表表单管理页面-SNF快速开发平台3.3-Spring.Net.Framework


下一篇:吴恩达机器学习笔记6-梯度下降II(Gradient descent intuition)--梯度下降的直观理解