Import Org.w3c.dom.nodelist;import Javax.xml.parsers.documentbuilder;import Javax.xml.parsers.documentbuilderfactory;import Java.io.inputstream;import Java.io.outputstream;import Java.io.outputstreamwriter;import java.net.url;import java.net.urlconnection;/** * @Author: VIC * @Description: Cell Phone Attribution Query */public class Phoneaddressutil {private static string Getsoaprequest (String mobilecode) {Stringbuilde R 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 "+" <mo Bilecode> "+ Mobilecode +" </mobileCode> "+" \ n " + "<userID></userID>" + "\ n" + "</getMobileCodeInfo>" + "\ n" + "&L T;/soap:body> "+" \ n "+" </soap:Envelope> "); return sb.tostring (); } private static InputStream Getsoapinputstream (string mobilecode) {try {string soap = Getsoapreque St (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 (); Osw.close (); InputStream is = Conn.getinputstream (); return is; } catch (Exception e) {e.printstacktrace (); return null; }}//Return example Anhui anqing Anhui unicom GSM card 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 ("Cell phone number Error")) {sb = new StringBuffer ("#"); Break } sb.append (N.getfirstchild (). Getnodevalue () + "\ n"); } is.close (); return sb.tostring (); } catch (Exception e) {e.printstacktrace (); return null; }}//Get the phone belongs to the province public static string Getmobileattribution (String mobile) {string str = ""; str = phoneaddressutil.getmobilenotrack (Mobile); if (str! = NULL &&! "". Equals (str)) {str = str.substring (Str.indexof (":") +1); String Strarry [] = new string[]{}; Strarry = Str.split (""); if (strarry.length >= 2) {str = strarry[0]+strarry[1]; } else {str = "no attribution information"; }} return str; }}
Mobile phone number attribution to query tool class