1 Packageorg.lebing.test;2 3 ImportJava.io.InputStream;4 ImportJava.io.OutputStream;5 ImportJava.io.OutputStreamWriter;6 ImportJava.net.URL;7 Importjava.net.URLConnection;8 9 ImportJavax.xml.parsers.DocumentBuilder;Ten Importjavax.xml.parsers.DocumentBuilderFactory; One A Importorg.w3c.dom.NodeList; - - Public classTest { the /** - * - * Get SOAP requests - * + * @paramMobilecode - * + * @return A */ at - Private Staticstring Getsoaprequest (String mobilecode) { - -StringBuilder SB =NewStringBuilder (); - -Sb.append ("<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>" in+ "\ n" - to+ "<soap:envelope xmlns:xsi=\" http://www.w3.org/2001/xmlschema-instance\ "" ++ " " - the+ "xmlns:xsd=\" Http://www.w3.org/2001/xmlschema\ "" *+ " " $ Panax Notoginseng+ "xmlns:soap=\" http://schemas.xmlsoap.org/soap/envelope/\ ">" -+ "\ n" the ++ "<soap:Body>" + "\ n" A the+ "<getmobilecodeinfo" + "" " ++ "xmlns=\" http://webxml.com.cn/\ ">" + "\ n" - $+ "<mobileCode>" + Mobilecode + "</mobileCode>" + "\ n" $ -+ "<userID></userID>" + "\ n" - the+ "</getMobileCodeInfo>" + "\ n" - Wuyi+ "</soap:Body>" + "\ n" the -+ "</soap:Envelope>" Wu - ); About $ returnsb.tostring (); - - } - A /** + * the * Send a SOAP request to the server and accept the return data - * $ * @paramMobilecode the * the * @return the */ the - Private StaticInputStream Getsoapinputstream (String mobilecode) { in the Try { the AboutString soap =getsoaprequest (mobilecode); the the if(Soap = =NULL) the + return NULL; - theURL url =NewURL (Bayi"Http://www.webxml.com.cn/WebServices/MobileCodeWS.asmx"); the theURLConnection conn =url.openconnection (); - -Conn.setusecaches (false); the theConn.setdoinput (true); the theConn.setdooutput (true); - theConn.setrequestproperty ("Content-type", "text/xml; Charset=utf-8 "); the theConn.setrequestproperty ("Content-length", integer.tostring (soap94 . Length ())); the theConn.setrequestproperty ("SOAPAction", the"Http://WebXml.com.cn/getMobileCodeInfo");98 AboutOutputStream OS =Conn.getoutputstream (); - 101OutputStreamWriter OSW =NewOutputStreamWriter (OS, "UTF-8"));102 103 osw.write (soap);104 the Osw.flush ();106 107 osw.close ();108 109InputStream is =Conn.getinputstream (); the 111 returnis ; the 113}Catch(Exception e) { the the e.printstacktrace (); the 117 return NULL;118 119 } - 121 }122 123 Public Staticstring Getmobilenotrack (String mobilecode) {124 the Try {126 127Org.w3c.dom.Document Document =NULL; - 129Documentbuilderfactory DBF =documentbuilderfactory.newinstance (); the 131Dbf.setnamespaceaware (true); the 133InputStream is =Getsoapinputstream (mobilecode);134 135Documentbuilder db =Dbf.newdocumentbuilder ();136 137Document =Db.parse (IS);138 139NodeList NL =Document $. getElementsByTagName ("Getmobilecodeinforesult");141 142StringBuffer SB =NewStringBuffer ();143 144 for(inti = 0; I < nl.getlength (); i++) {145 146Org.w3c.dom.Node n =Nl.item (i);147 148 if(N.getfirstchild (). Getnodevalue (). Equals ("Phone number error")) {149 MaxSB =NewStringBuffer ("#");151 theSYSTEM.OUT.PRINTLN ("Phone number entered incorrectly");153 154 Break;155 156 }157 158Sb.append (N.getfirstchild (). Getnodevalue () + "\ n");159 the }161 162 is.close ();163 164 returnsb.tostring ();165 166}Catch(Exception e) {167 168 e.printstacktrace ();169 the return NULL;171 172 }173 174 175 }176 Public Static voidMain (string[] args) {177 //System.out.println (Test.getsoaprequest ("13272303204"));178 //System.out.println (Test.getsoapinputstream ("13226678785"));179System.out.println (Test.getmobilenotrack ("13111111111")); the }181 182 183}
Direct copy of the code is OK
Java Get phone number attribution