java语言实现号码归属地查询

package cn.inspur;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Scanner;

import org.junit.Test;

public class Demo_Mobile {
	@Test
	public void test() throws IOException {
		// 1:声明地址
		Scanner scanner = new Scanner(System.in);
		String tel = scanner.nextLine();
		URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=" + tel
				+ "&userID=");// 从webxml.com.cn找到的
		// 2:获取连接
		HttpURLConnection con = (HttpURLConnection) url.openConnection();
		con.setRequestMethod("GET");
		con.setDoOutput(true);// 可以发数据
		con.setDoInput(true);// 可以读取返回的数据
		con.connect();
		// 获取连接状态
		int code = con.getResponseCode();
		if (code == 200) {
			// 连接成功,获取服务器返回IO
			InputStream in = con.getInputStream();
			byte[] bs = new byte[1024];
			int len = 0;
			while ((len = in.read(bs)) != -1) {
				String str = new String(bs, 0, len);
				System.err.print(str);
			}
			in.close();
		}
		con.disconnect();// 断开连接
	}
}

猜你喜欢

转载自blog.csdn.net/qq_33160365/article/details/51272163