Preface:
Use background: In the project use process, the customer needs the project to connect the mobile, the Unicom, the telecommunication three big carrier short message transmission function, but for the non-local (here for Shanghai) mobile number, because the authority cannot send, therefore requests in the system page to the handset number attribution place to mark.
Use process:
There are probably two ways of distinguishing between finding the data,
1th: Through the httpclient to some provide mobile phone number of the site to query the Web page into the mobile phone number, the return of the content based on the analysis of the results, but this method in person to test, occasionally successful, most of the case will be reported to parse error.
2nd: Found in the Baidu Library, through the http://www.webxml.com.cn website provided by the Webserivice service return results, testing was very successful, the code is very concise, written as a tool class, the code is as follows.
Package com.founder.fwpt.util;
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; /** * Mobile phone number attribution to query Tools * @author Zhaozuofa * 2015-11-3 pm 7:07:25 */public class Judemobileattribution {private St
Atic 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:so Ap=\ "http://schemas.xmlsoap.org/soap/envelope/\" > "+" \ n "+" <soap:Body> "+" \ n "+" <getmobile Codeinfo "+" "+" 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 = Getsoapr
Equest (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; } public static string Getmobilenotrack (String mobilecode) {try {org.w3c.dom.Document does
Cument = 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 ("Mobile number Error") {sb = new StringBuffer ("#");
System.out.println ("mobile phone number entered incorrectly");
Break
} sb.append (N.getfirstchild (). Getnodevalue () + "\ n");
} is.close ();
return sb.tostring ();
catch (Exception e) {e.printstacktrace ();
return null;
} public static string Getmobileattribution (String Mobile) {string str = "";
str = judemobileattribution.getmobilenotrack (Mobile); if (str!= null &&! "".
Equals (str)) {str = str.substring (Str.indexof (":") +1);
String Strarry [] = new string[]{};
Strarry = Str.split ("");
str = strarry[0];
return str; }/* public static void Main (string[] args) {//System.out.println (Moblie.getsoaprequest ("13272303204 "));
System.out.println (Moblie.getsoapinputstream ("13226678785"));
System.out.println (Judemobileattribution.getmobilenotrack ("13052297710"));
Judemobileattribution.getmobileattribution ("13052297710");
}*/
}