利用webxml網站提供的webservice查詢手機號碼歸屬地

和JAVA網絡xx差很少,你懂得!免費用戶天天只能使用100次!不過你要是搞個for循環1000次 一時半會它們阻止不了 java

JAVA  webservice  想用免費的無限次數的點擊:http://my.oschina.net/jgy/blog/86305 node

package wiselong;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Mobile {
	/**
	 * 得到soap請求
	 * @param mobileCode 手機號碼
	 * @return  soap請求
	 * @author JIA-G-Y
	 */
	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();
	}
	/**
	 * 發送soap請求到服務器,並接受返回數據
	 * @param mobileCode  手機號碼
	 * @return  返回的數據
	 * @author JIA-G-Y
	 */
	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;
		}
	}
	/**
	 * 查詢手機號碼歸屬地
	 * @param mobileCode  手機號碼
	 * @return
	 * @author JIA-G-Y
	 */
	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 nodeList = document.getElementsByTagName("getMobileCodeInfoResult");
			StringBuffer sb = new StringBuffer();
			for(int i = 0; i < nodeList.getLength(); i++){
				Node node = nodeList.item(i);
				sb.append(node.getFirstChild().getNodeValue());
			}
			is.close();
			return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	public static void main(String[] args) {
		String tel = "13524799521";
		Pattern pattern = Pattern.compile("1\\d{10}");
		Matcher matcher = pattern.matcher(tel);
		if(matcher.matches()){
			System.err.println(getMobileNoTrack(tel));
		}else{
			System.err.println("手機號碼格式錯誤");
		}
	}
}
相關文章
相關標籤/搜索