引入jar包
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.8.0</version>
</dependency>
直接一个工具类
package sys.cdsoft.util;
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class PhoneAdress {
/**
* 传入手机号 0 错误 1 移动 2联通 3电信 完整返回 山东临沂 中国联通
* @param phone
* @return
*/
public static String getPhoneAdress(String phone) {
try {
OkHttpClient client = new OkHttpClient().newBuilder().build();
Request request = new Request.Builder().url("https://www.sogou.com/websearch/phoneAddress.jsp?phoneNumber="+phone)
.method("GET",null).addHeader("Content-Type", "application").build();
Response response = client.newCall(request).execute();
String result = response.body().string();
if(result.indexOf("移动") >= 0) {
return "1";
}else if(result.indexOf("联通") >= 0) {
return "2";
}else {
return "3";
}
}catch(Exception e) {
return "0";
}
}
public static void main(String[] args) throws IOException {
String phoneAdress = getPhoneAdress("17862863203");
if(phoneAdress.equals("0")) {
System.out.println("======查询错误======");
}else {
System.out.println(phoneAdress);
}
}
}