There are already a lot of mobile phone numbers on the network to query the API interface, but these interfaces are always some large and small defects.
Summarize these flaws:
1, to directly paste its search box link form to their own page, click on the query when you want to jump to their site to show the results of attribution
2, the API to provide the interface, generally require payment, or a day only free limited number of queries
3, some blog documents in the API is too old, when trying, has been 404Not found
So write a blog, for the phone is doing the attribution of the small partner reference.
Ideas:
I found a pat NET interface, can be used by Curl Direct mobile phone number to query, and will return to us a JSON-like string (actually not JSON, that is, some strings have the information we want)
->java through HttpURLConnection to connect to this address, and crawl to the returned page of all the strings, these strings contain the above-mentioned class JSON results
Let's get this string and parse the information we want from the communications provider and the province city.
Description
Pat Web Check mobile phone attribution address:http:
//virtual.paipai.com/extinfo/GetMobileProductInfo?mobile=15850781443&amount=10000
Parameter description:mobile:手机号码
amount:未知(但是必须要有,不然查询不出结果)
返回值:类似JSON的字符串
Specific implementation:
1 /**2 * @ClassName: Httpclientutil3 * @Description: HTML page fetching is known as the String tool class4 * @author: Chenkaideng5 * @date November 2, 2015 PM 3:55:496 */7 8 ImportJava.io.BufferedReader;9 ImportJava.io.InputStream;Ten ImportJava.io.InputStreamReader; One Importjava.net.HttpURLConnection; A ImportJava.net.URL; - Importorg.apache.commons.io.IOUtils; - Importorg.apache.commons.lang3.StringUtils; the ImportOrg.slf4j.Logger; - Importorg.slf4j.LoggerFactory; - - + Public classHttpclientutil { - Private Static FinalLogger Logger = Loggerfactory.getlogger ("HttpClient"); + PrivateString Readinputstream (InputStream instream, String charest)throwsException { AStringBuilder SB =NewStringBuilder (); at Try( -InputStreamReader ISR =NewInputStreamReader (instream, charest); -BufferedReader reader =NewBufferedReader (ISR);) { -String line =NULL; - while(line = Reader.readline ())! =NULL) { - Sb.append (line); in } - } to returnsb.tostring (); + - } the * Publicstring Getwebcontent (String webUrl, String charest) { $ if(Stringutils.isempty (WEBURL))Panax Notoginseng return NULL; - intResponse =-1; theHttpURLConnection conn =NULL; + Try { AURL url =NewURL (WEBURL); theconn =(HttpURLConnection) url.openconnection (); +Conn.setrequestmethod ("GET"); -Conn.setreadtimeout (60 * 2000); $Conn.setconnecttimeout (10 * 1000); $Conn.setrequestproperty ("User-agent", "mozilla/5.0" (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/45.0.2454.101 safari/537.36 "); -Conn.setdooutput (true); - Conn.connect (); theResponse =Conn.getresponsecode (); - if(Response = = 200) {WuyiInputStream im =NULL; the Try { -im =Conn.getinputstream (); Wu returnReadinputstream (IM, charest); -}finally { About ioutils.closequietly (IM); $ } - } - return NULL; -}Catch(Exception e) { ALogger.error (String.Format ("error downloading to file [url=%s][%s][responsecode=%d]", WebUrl, E.getmessage (), response)); + return NULL; the}finally { - if(Conn! =NULL) { $ Conn.disconnect (); theconn =NULL; the } the } the } -}
Then call the above tool class, with the mobile phone number parameters to access the PAT interface address, caught the page, to parse out the attribution information can be
1 ImportCom.alibaba.fastjson.JSONObject;2 /**3 * 4 * @ClassName: Getmobilemessage5 * @Description: TODO6 * @author[email protected]7 * @date January 28, 2016 PM 2:40:568 *9 */Ten Public classgetmobilemessage{ One Private Static FinalString phone_place_api_url= "Http://virtual.paipai.com/extinfo/GetMobileProductInfo"; A /** - * - * @Title: Getmobileplace the * @Description: Get information about your phone's place of attribution - * @param @paramMobile - * @param @return - * @returnString + * @throws - */ + Publicstring Getmobileplace (String mobile) { AHttpclientutil util =Newhttpclientutil (); atString[] strings={"", "" "}; - Try { - //access to the query interface of the PAT -String mobilemessage = util.getwebcontent (phone_place_api_url+ "? mobile=" +mobile+ "&amount=10000", "GB2312"); -strings = Mobilemessage.split (";"); - //(the page gets to the message, in addition to these, there are some HTML statements) in //string[0]= "({Mobile: ' 15850781443 ', province: ' Jiangsu ', ISP: ' China Mobile ', Stock: ' 1 ', Amount: ' 10000 ', Maxprice: ' 0 ', Minprice: ' 0 ', CityName: ' Nanjing '} '; -Mobilemessage = Strings[0]; toJsonobject jsonobject = Jsonobject.parseobject (mobilemessage.substring (1, Mobilemessage.length ()-1)); + //analyze the provinces and city and operators -String province = jsonobject.getstring ("province"); theString CityName = jsonobject.getstring ("CityName"); *String ISP = jsonobject.getstring ("ISP"); $ returnisp+ "+province+"CityName;Panax Notoginseng}Catch(Exception e) { - e.printstacktrace (); the //Logger.error (strings[0]+e.tostring ()); + return""; A } the } +}
This can be free to get the phone number of the attribution of information, and can be used as a tool for their own use, we love how to encapsulate how to encapsulate,
Otherwise check a place to charge also to others website advertising, it is very uncomfortable ah.
But the only flaw is that if you change the address, you have to change it.
But it doesn't matter, all give the whole idea, what address what interface can complete the attribution place.
"Original" Java implementation of mobile phone number attribution to query