jsp代碼:
<form action="https://tcc.taobao.com/cc/json/mobile_tel_segment.htm" method="post">
? ? ? ? ?請輸入手機號:<input type="text" name="tel" value="">
? ? ? ? ? ? ? ? <input type="submit" value="查詢 ">
</form>
訪問jsp頁面,輸入測試手機號碼:13535382**2,點擊查詢按鈕,得到以下返回結果
__GetZoneResult_ = {
? ? mts:'1353538',
? ? province:'廣東',
? ? catName:'中國移動',
? ? telString:'13535382112',
? ? areaVid:'30517',
? ? ispVid:'3236139',
? ? carrier:'廣東移動'
}
?
通過JAVA代碼訪問:
package com.interfaces.demo1;
?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
?
public class InterfaceTest {
? ? public static void main(String[] args) throws Exception {
? ? ? ? // 方法一
? ? ? ? System.out.println(InterfaceTest.getURLContent());
? ? ? ? // 方法二
? ? ? ? String urlStr = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13535382112";
? ? ? ? System.out.println(InterfaceTest.getURLContent(urlStr));
? ? }
?
? ? public static String getURLContent() throws Exception {
? ? ? ? String strURL = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13535382112";
? ? ? ? URL url = new URL(strURL);
? ? ? ? HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
? ? ? ? httpConn.setRequestMethod("GET");
? ? ? ? httpConn.connect();
?
? ? ? ? BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
? ? ? ? String line;
? ? ? ? StringBuffer buffer = new StringBuffer();
? ? ? ? while ((line = reader.readLine()) != null) {
? ? ? ? ? ? buffer.append(line);
? ? ? ? }
? ? ? ? reader.close();
? ? ? ? httpConn.disconnect();
? ? ? ? System.out.println(buffer.toString());
? ? ? ? System.out.println(buffer);
? ? ? ? System.out.println(buffer.toString());
? ? ? ? return buffer.toString();
? ? }
?
? ? /**
? ? ?* 程序中訪問http數據接口
? ? ?*/
? ? public static String getURLContent(String urlStr) {
? ? ? ? /** 網絡的url地址 */
? ? ? ? URL url = null;
? ? ? ? /** http連接 */
? ? ? ? HttpURLConnection httpConn = null;
? ? ? ? /**//** 輸入流 */
? ? ? ? BufferedReader in = null;
? ? ? ? StringBuffer sb = new StringBuffer();
? ? ? ? try {
? ? ? ? ? ? url = new URL(urlStr);
? ? ? ? ? ? in = new BufferedReader(new InputStreamReader(url.openStream(), "GBk"));
? ? ? ? ? ? String str = null;
? ? ? ? ? ? while ((str = in.readLine()) != null) {
? ? ? ? ? ? ? ? sb.append(str);
? ? ? ? ? ? }
? ? ? ? } catch (Exception ex) {
?
? ? ? ? } finally {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? if (in != null) {
? ? ? ? ? ? ? ? ? ? in.close();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? } catch (IOException ex) {
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? String result = sb.toString();
? ? ? ? return result;
? ? }
}
?
?