需求:
用户输入根据手机号,获得手机号所在的省市信息
思路:
调用相关的API进行分析
效果
代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using LitJson;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
public class PhoneInfo
{
/// <summary>
/// </summary>
public PhoneInfo()
{
}
public string Mobile;
public string QueryResult;
public string TO;
/// <summary>
/// 中国联通
/// </summary>
public string Corp;
public string Province;
public string City;
/// <summary>
/// 区域编号
/// </summary>
public string AreaCode;
public string PostCode;
public string VNO;
public string Card;
public override string ToString()
{
return string.Format("手机号:{0} 运营商:{1} 省:{2} 市:{3} 区号:{4}", Mobile, Corp, Province, City, AreaCode);
}
}
class Program
{
static void Main(string[] args)
{
Regex rx = new Regex(@"^[1][358][0-9]{9}$");
var phoneNum = "13262983383";
if (rx.IsMatch(phoneNum))
{
var Url = @"http://v.showji.com/Locating/showji.com2016234999234.aspx?m=13262983386&output=json&callback=querycallback×tamp=1480066397101";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
//字符串截取
var trueValue = retString.Substring(14, retString.Length - 16);
try
{
var phone = JsonMapper.ToObject<PhoneInfo>(trueValue);
Console.WriteLine(phone);
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.Read();
}
}
}
}