import java.io.InputStream
import java.io.OutputStream
import java.io.OutputStreamWriter
import java.net.URL
import java.net.URLConnection
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
import org.w3c.dom.NodeList
public class Mobile {
private static String getSoapRequest(String mobileCode) {
StringBuilder sb = new StringBuilder()
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "\n"
+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
+ " "
+ "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\""
+ " "
+ "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ "\n"
+ "<soap:Body>" + "\n"
+ "<getMobileCodeInfo" + " "
+ "xmlns=\"http://WebXml.com.cn/\">" + "\n"
+ "<mobileCode>" + mobileCode + "</mobileCode>" + "\n"
+ "<userID></userID>" + "\n"
+ "</getMobileCodeInfo>" + "\n"
+ "</soap:Body>" + "\n"
+ "</soap:Envelope>"
)
return sb.toString()
}
private static InputStream getSoapInputStream(String mobileCode) {
try {
String soap = getSoapRequest(mobileCode)
if (soap == null)
return null
URL url = new URL(
"http://www.webxml.com.cn/WebServices/MobileCodeWS.asmx")
URLConnection conn = url.openConnection()
conn.setUseCaches(false)
conn.setDoInput(true)
conn.setDoOutput(true)
conn.setRequestProperty("Content-Type", "text/xml charset=utf-8")
conn.setRequestProperty("Content-Length", Integer.toString(soap
.length()))
conn.setRequestProperty("SOAPAction",
"http://WebXml.com.cn/getMobileCodeInfo")
OutputStream os = conn.getOutputStream()
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8")
osw.write(soap)
osw.flush()
osw.close()
InputStream is = conn.getInputStream()
return is
} catch (Exception e) {
e.printStackTrace()
return null
}
}
public static String getMobileNoTrack(String mobileCode) {
try {
org.w3c.dom.Document document = null
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance()
dbf.setNamespaceAware(true)
InputStream is = getSoapInputStream(mobileCode)
DocumentBuilder db = dbf.newDocumentBuilder()
document = db.parse(is)
NodeList nl = document
.getElementsByTagName("getMobileCodeInfoResult")
StringBuffer sb = new StringBuffer()
for (int i = 0 i < nl.getLength() i++) {
org.w3c.dom.Node n = nl.item(i)
if (n.getFirstChild().getNodeValue().equals("手机号码错误")) {
sb = new StringBuffer("#")
System.out.println("手机号码输入有误")
break
}
sb.append(n.getFirstChild().getNodeValue() + "\n")
}
is.close()
return sb.toString()
} catch (Exception e) {
e.printStackTrace()
return null
}
}
public static void main(String[] args) {
// System.out.println(Moblie.getSoapRequest("13272303204"))
// System.out.println(Moblie.getSoapInputStream("13226678785"))
System.out.println(Mobile.getMobileNoTrack("1583759999"))
}
}
这段代码也是我在网上找的,然后在Myeclipse8.5M2中测试通过的根据输入的电话号码,可以查询号码归属地.不知道是否你需要的答案...
import java.io.IOExceptionimport org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.select.Elements
public class Test {
public static void main(String[] args) throws IOException {
String mobile = "手机号码"
String url = "http://www.ip138.com:8080/search.asp?action=mobile&mobile=%s"
url = String.format(url, mobile)
Document doc = Jsoup.connect(url).get()
Elements els = doc.getElementsByClass("tdc2")
System.out.println("归属地:" + els.get(1).text())
System.out.println("类型:" + els.get(2).text())
System.out.println("区号:" + els.get(3).text())
System.out.println("邮编:" + els.get(4).text().substring(0, 6))
}
}
JAVA获取手机号码归属地信息工具包,使用org.osm.mobile.MobileUtil.getMobileInfo(String mobile)方法获取信息,返回Mobile对象,对象包括以下属性:String number = ""// 手机号码
String province = ""// 省份
String city = ""// 城市
String areaCode = ""// 城市编码
String postCode = ""// 邮政号码
String corp = ""// 运营商
String card = ""// 卡类型