Locate the home location using the mobile phone number and the location of the mobile phone number
At work, many places only know the mobile phone number and want to know the location where the mobile phone number is located.
This feature allows you to achieve this requirement. At the same time, you can make adjustments on this basis to implement other functions, such as creating your own mobile phone number segment ownership database...
Implementation: Obtain the returned information through the free query interface provided on the Internet, parse the returned information, and obtain the data you need.
Use the control: LitJson. dll (download LitJson. dll or search online)
The Code is as follows:
/// <Summary> /// obtain the url return value /// </summary> /// <param name = "url"> eg: http://m.weather.com.cn/atad/101010100.html </param> private string RequestGetInfo (string url) {string strBuff = ""; Uri httpURL = new Uri (url); // The HttpWebRequest class inherits from WebRequest, you do not have your own constructor. You need to use the Creat method of WebRequest to establish the function and forcibly convert the type to HttpWebRequest httpReq = (HttpWebRequest) WebRequest. create (httpURL); // Create Ht using the GetResponse () method of HttpWebRequest TpWebResponse, forced type conversion HttpWebResponse httpResp = (HttpWebResponse) httpReq. getResponse (); // GetResponseStream () method to get the HTTP response data stream and try to get the webpage content specified in the URL. // If the webpage content is obtained successfully, System is used. IO. returns in Stream format. If it fails, the ProtoclViolationException error is generated. In this case, the following code should be put into a try block for processing. Stream respStream = httpResp. getResponseStream (); // The returned content is in the Stream format. Therefore, you can use the StreamReader class to obtain the content of GetResponseStream, read the content of each row of the source code of the web page in sequence using the Read method of the // StreamReader class until the end of the line (Read Encoding format: UTF8) StreamReader respStreamReader = new StreamReader (respStream, Encoding. UTF8); strBuff = respStreamReader. readToEnd (); return strBuff ;}
Call code:
String txtPhone = this.txt Mobile. Text; // enter the phone number string url = "http://v.showji.com/Locating/showji.com2016234999234.aspx? Output = json & callback = querycallback & timestamp = "+ GetNowTimestamp () +" & m = "+ txtPhone; string get = RequestGetInfo (url); get = get. substring (get. indexOf ('{'); get = get. substring (0, get. indexOf ('}') + 1); JsonData jd = JsonMapper. toObject (get); string result = jd ["QueryResult"]. toString (); string city = jd ["City"]. toString (); // belongs to the MessageBox city. show (get );
The timestamp of GetNowTimestamp () is used to ensure different values.
private string GetNowTimestamp() { TimeSpan span = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime(); return span.TotalSeconds.ToString(); }
If you enter the Mobile phone number: 13800138000 (in fact, you only need the first seven digits of the Mobile phone number), The get value returns the string: "{" Mobile ":" 13800138000 "," QueryResult ":" True ", "TO": "China Mobile", "Corp": "China Mobile", "Province": "Beijing", "City": "Beijing", "AreaCode": "010 ", "PostCode": "100000", "VNO": "", "Card ":""}.
Mobile is the Mobile phone number (segment)
QueryResult is the query result, and = True indicates that the query is successful. = False indicates that the query fails.
Corp is the company of the number
Province is the Province
City is the City
AreadCode is the region code
PostCode is the location zip code
Other information is unknown.
The problem with using LitJson is that when it converts a JSON object to a string, it automatically converts Chinese to Unicode encoding. It is not intuitive enough to save it to a file. Therefore, a transcoding processing function between Unicode and normal strings is provided:
/// <Summary> // convert the string to Unicode // </summary> /// <param name = "source"> source string </ param> // <returns> Unicode encoded string </returns> public static string String2Unicode (string source) {byte [] bytes = Encoding. unicode. getBytes (source); StringBuilder stringBuilder = new StringBuilder (); for (int I = 0; I <bytes. length; I + = 2) {stringBuilder. appendFormat ("\ u {0} {1}", bytes [I + 1]. toString ("x "). padLeft (2, '0'), bytes [I]. toString ("x "). padLeft (2, '0');} return stringBuilder. toString ();} /// <summary> // convert Unicode to string /// </summary> /// <param name = "source"> Unicode-encoded string </param>/ // <returns> normal string </returns> public static string Unicode2String (string source) {return new Regex (@ "\ u ([0-9A-F] {4})", RegexOptions. ignoreCase | RegexOptions. compiled ). replace (source, x => string. empty + Convert. toChar (Convert. toUInt16 (x. result ("$1"), 16 )));}
Call:
// ConfigFile is the File name // jdNew is the JSON object // jdNew. ToJson () converts the JSON object into a string File. WriteAllText (configFile, Unicode2String (jdNew. ToJson ()));
The above code is obtained online and shared with you, hoping to be useful to you.
You can use your imagination to make things you need.