java系统怎么设置号码归属地
2个回答
展开全部
package test;
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中测试通过的;根据输入的电话号码,可以查询号码归属地.不知道是否你需要的答案...
Storm代理
2023-07-25 广告
2023-07-25 广告
StormProxies是一家提供动态代理服务器服务的企业,旨在帮助用户更好地管理网络访问和安全。以下是一些关于StormProxies的IP动态代理服务的特点:1. 高匿名性:StormProxies的动态代理服务器具有高匿名性,可以有效...
点击进入详情页
本回答由Storm代理提供
展开全部
java中利用爬虫的思路去完成这个功能。这里大概思路是通过HttpClient去模拟提交那些网站的查询功能,这里是www.ip138.com,然后通过正则表达式去解析HttpClient相应内容,从里面抽取出手机归属地。同时对要查询的手机进行一个验证,具体代码请看如下:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
/**
* 通过手机号码,获得该号码的归属地
*
* @author Administrator
*
*/
public class MobileFromUtil {
//正则表达式,抽取手机归属地
public static final String REGEX_GET_MOBILE=
"(?is)(<tr[^>]+>[\\s]*<td[^>]+>[\\s]*卡号归属地[\\s]*</td>[\\s]*<td[^>]+>([^<]+)</td>[\\s]*</tr>)"; //2:from
//正则表达式,审核要获取手机归属地的手机是否符合格式,可以只输入手机号码前7位
public static final String REGEX_IS_MOBILE=
"(?is)(^1[3|4|5|8][0-9]\\d{4,8}$)";
/**
* 获得手机号码归属地
*
* @param mobileNumber
* @return
* @throws Exception
*/
public static String getMobileFrom(String mobileNumber) throws Exception {
if(!veriyMobile(mobileNumber)){
throw new Exception("不是完整的11位手机号或者正确的手机号前七位");
}
HttpClient client=null;
PostMethod method=null;
NameValuePair mobileParameter=null;
NameValuePair actionParameter=null;
int httpStatusCode;
String htmlSource=null;
String result=null;
try {
client=new HttpClient();
client.getHostConfiguration().setHost("www.ip138.com", 8080, "http");
method=new PostMethod("/search.asp");
mobileParameter=new NameValuePair("mobile",mobileNumber);
actionParameter=new NameValuePair("action","mobile");
method.setRequestBody(new NameValuePair[] { actionParameter,mobileParameter });
//设置编码
method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "GB2312");
client.executeMethod(method);
httpStatusCode=method.getStatusLine().getStatusCode();
if(httpStatusCode!=200){
throw new Exception("网页内容获取异常!Http Status Code:"+httpStatusCode);
}
htmlSource=method.getResponseBodyAsString();
if(htmlSource!=null&&!htmlSource.equals("")){
result=parseMobileFrom(htmlSource);
}
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
method.releaseConnection();
}
return result;
}
/**
* 从www.ip138.com返回的结果网页内容中获取手机号码归属地,结果为:省份 城市
*
* @param htmlSource
* @return
*/
public static String parseMobileFrom(String htmlSource){
Pattern p=null;
Matcher m=null;
String result=null;
p=Pattern.compile(REGEX_GET_MOBILE);
m=p.matcher(htmlSource);
while(m.find()){
if(m.start(2)>0){
result=m.group(2);
result=result.replaceAll(" ", " ");
}
}
return result;
}
/**
* 验证手机号
* @param mobileNumber
* @return
*/
public static boolean veriyMobile(String mobileNumber){
Pattern p=null;
Matcher m=null;
p=Pattern.compile(REGEX_IS_MOBILE);
m=p.matcher(mobileNumber);
return m.matches();
}
/**
* 测试
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
System.out.println(getMobileFrom("13888888888"));
}
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
/**
* 通过手机号码,获得该号码的归属地
*
* @author Administrator
*
*/
public class MobileFromUtil {
//正则表达式,抽取手机归属地
public static final String REGEX_GET_MOBILE=
"(?is)(<tr[^>]+>[\\s]*<td[^>]+>[\\s]*卡号归属地[\\s]*</td>[\\s]*<td[^>]+>([^<]+)</td>[\\s]*</tr>)"; //2:from
//正则表达式,审核要获取手机归属地的手机是否符合格式,可以只输入手机号码前7位
public static final String REGEX_IS_MOBILE=
"(?is)(^1[3|4|5|8][0-9]\\d{4,8}$)";
/**
* 获得手机号码归属地
*
* @param mobileNumber
* @return
* @throws Exception
*/
public static String getMobileFrom(String mobileNumber) throws Exception {
if(!veriyMobile(mobileNumber)){
throw new Exception("不是完整的11位手机号或者正确的手机号前七位");
}
HttpClient client=null;
PostMethod method=null;
NameValuePair mobileParameter=null;
NameValuePair actionParameter=null;
int httpStatusCode;
String htmlSource=null;
String result=null;
try {
client=new HttpClient();
client.getHostConfiguration().setHost("www.ip138.com", 8080, "http");
method=new PostMethod("/search.asp");
mobileParameter=new NameValuePair("mobile",mobileNumber);
actionParameter=new NameValuePair("action","mobile");
method.setRequestBody(new NameValuePair[] { actionParameter,mobileParameter });
//设置编码
method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "GB2312");
client.executeMethod(method);
httpStatusCode=method.getStatusLine().getStatusCode();
if(httpStatusCode!=200){
throw new Exception("网页内容获取异常!Http Status Code:"+httpStatusCode);
}
htmlSource=method.getResponseBodyAsString();
if(htmlSource!=null&&!htmlSource.equals("")){
result=parseMobileFrom(htmlSource);
}
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
method.releaseConnection();
}
return result;
}
/**
* 从www.ip138.com返回的结果网页内容中获取手机号码归属地,结果为:省份 城市
*
* @param htmlSource
* @return
*/
public static String parseMobileFrom(String htmlSource){
Pattern p=null;
Matcher m=null;
String result=null;
p=Pattern.compile(REGEX_GET_MOBILE);
m=p.matcher(htmlSource);
while(m.find()){
if(m.start(2)>0){
result=m.group(2);
result=result.replaceAll(" ", " ");
}
}
return result;
}
/**
* 验证手机号
* @param mobileNumber
* @return
*/
public static boolean veriyMobile(String mobileNumber){
Pattern p=null;
Matcher m=null;
p=Pattern.compile(REGEX_IS_MOBILE);
m=p.matcher(mobileNumber);
return m.matches();
}
/**
* 测试
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
System.out.println(getMobileFrom("13888888888"));
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询