java查询手机号码归属地_Java实现手机号码归属地查询

?package com.rocky.test;

import java.io.InputStream;

import java.io.OutputStream;

import java.io.OutputStreamWriter;

import http://www.doczj.com/doc/973d8e6c27d3240c8447ef57.html.URL;

import http://www.doczj.com/doc/973d8e6c27d3240c8447ef57.html.URLConnection;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.NodeList;

public class Moblie {

/**

*

* 获得soap请求

*

* @param mobileCode

*

* @return

*/

private static String getSoapRequest(String mobileCode) {

StringBuilder sb = new StringBuilder();

sb.append(""

+ "\n"

+ "+ " "

+ "xmlns:xsd=\"http://www.doczj.com/doc/973d8e6c27d3240c8447ef57.html/2001/XMLSchema\""

+ " "

+ "xmlns:soap=\"http://www.doczj.com/doc/973d8e6c27d3240c8447ef57.html/soap/envelope/\">"

+ "\n"

+ "" + "\n"

+ "+ "xmlns=\"http://www.doczj.com/doc/973d8e6c27d3240c8447ef57.html/\">" + "\n"

+ "" + mobileCode + "" + "\n"

+ "" + "\n"

+ "" + "\n"

+ "" + "\n"

+ ""

);

return sb.toString();

}

/**

*

* 发送soap请求到服务器,并接受返回数据

*

* @param mobileCode

*

* @return

*/

private static InputStream getSoapInputStream(String mobileCode) {

try {

String soap = getSoapRequest(mobileCode);

if (soap == null)

return null;

URL url = new URL(

"http://www.doczj.com/doc/973d8e6c27d3240c8447ef57.html/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://www.doczj.com/doc/973d8e6c27d3240c8447ef57.html/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("手机号码错误")) {

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值