java 获取手机归属地,引起net.UnknownHostException错误

时间:2023-03-09 06:14:53
java 获取手机归属地,引起net.UnknownHostException错误

这个问题是请求,重定向了,跟入源码。修改了地址,变成302

                Connection connect = Jsoup.connect(url);
connect.header("Host", "http://info.bet007.com");
connect.header("User-Agent", " Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0");
connect.header("Accept", " text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
connect.header("Accept-Language", "zh-cn,zh;q=0.5");
connect.header("Accept-Charset", " GB2312,utf-8;q=0.7,*;q=0.7");
connect.header("Connection", "keep-alive"); Document doc = connect.get();

  就可以获取到手机归属地了

	public static String[] getPhoneAddressAndCompanyStrs(String phone){
if(phone.length()<=0)
return null;
String url = "http://www.ip138.com:8080/search.asp?action=mobile&mobile=%s";
url = String.format(url, phone); String[] strs=new String[2];
try {
Connection connect = Jsoup.connect(url);
connect.header("Host", "http://info.bet007.com");
connect.header("User-Agent", " Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0");
connect.header("Accept", " text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
connect.header("Accept-Language", "zh-cn,zh;q=0.5");
connect.header("Accept-Charset", " GB2312,utf-8;q=0.7,*;q=0.7");
connect.header("Connection", "keep-alive"); Document doc = connect.get(); Elements els = doc.getElementsByClass("tdc2");
int elsSize = els.size();
if(elsSize == 2) {
strs[0]=els.get(1).text();
}else if(elsSize > 2) {
strs[0]=els.get(1).text();
strs[1]=els.get(2).text();
}
return strs;
} catch (IOException e) { e.printStackTrace();
return null;
}
}