這段代碼是查詢指定手機號的代碼歸屬地,輸出格式爲【省份+運營商】
public static String checkMobilePlace(String mobilephone) throws IOException { //檢測手機號碼歸屬地 URL url = new URL( "http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=" + mobilephone + "&t=" + new Date().getTime()); URLConnection urlConnection = url.openConnection(); InputStream inputStream = urlConnection.getInputStream(); //取回查詢結果 String result=""; int c; while (((c = inputStream.read()) != -1)) { int all = inputStream.available(); byte[] b = new byte[all]; inputStream.read(b); result += new String(b, "GBK"); } HttpURLConnection httpConnection = (HttpURLConnection) urlConnection; String phoneadd = result.substring(55, 57)+result.substring(76, 78); System.out.println(phoneadd); return phoneadd; }
測試用例:
checkMobilePlace("15200033333");
運行結果:
河北移動