Java 实现手机号的归属地查询 手机号码的卡的类型
来源:互联网 发布:合理利用网络教学设计 编辑:程序博客网 时间:2024/05/21 17:22
package com.zhangsheng.contorller;
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 MobileFromUtil {
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(MobileFromUtil.getMobileNoTrack("13469896164"));
}
}
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 MobileFromUtil {
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(MobileFromUtil.getMobileNoTrack("13469896164"));
}
}
阅读全文
0 0
- Java 实现手机号的归属地查询 手机号码的卡的类型
- webservice-实现手机号码归属地的查询
- 手机号的归属地查询
- 手机号批量查询归属地的方法
- 如何利用java程序查询大量手机号码的归属地
- java 代码 利用手机号码查询,手机归属地、手机卡类型、地区的邮编
- java实现电话号码归属地的查询
- java程序代码实现手机号码归属地查询
- SOAP webService关于查询手机号归属地的运用
- 教你手机号批量查询归属地的方法
- java 手机号归属地查询
- [017] Android平台HttpClient的使用-手机号码归属地查询
- 网页抓取hao123的手机号码归属地查询
- Android平台HttpClient的使用-手机号码归属地查询
- [017] Android平台HttpClient的使用-手机号码归属地查询 .
- Android平台HttpClient的使用-手机号码归属地查询 (17)
- Android平台HttpClient的使用-手机号码归属地查询
- 36_查询手机号码归属地&外部数据库的引入
- Python woe 0.0.7 源码解析
- 函数柯里化的几个例子
- js函数内嵌函数的整体跳出
- 排序算法—归并排序
- 万圣节H5的经典案例,速收藏!
- Java 实现手机号的归属地查询 手机号码的卡的类型
- opencv学习笔记二
- MyBatis教程
- urllib.request模块(9/30)
- 从Eclipse转移到IntelliJ IDEA一点心得
- Java 抽象文档设计模式例子分析
- 文章标题
- Hasor-RSF —— 分布式服务框架
- LeetCode #76