您的位置:首页 > 编程语言 > Java开发

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

2016-04-28 10:16 441 查看
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();// 断开连接
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: