Call WebService to inquire about cell phone number attribution

Source: Internet
Author: User

WEB Services is an online application service published by an enterprise that accomplishes its specific business needs, and other companies or applications can access and use the online service over the Internet. Here we use the SOAP protocol to send information to the webservice, and then get the information returned by the WebService server, in order to query the cell phone number attribution information.

WEB Services has a lot of service providers, where we use www.webxml.com.cn,

Click to go in, we can see the contents of the SOAP protocol definition.

The next step is to write the code.

Package Cn.mzba.service; import Java.io.inputstream;import Java.io.outputstream;import Java.net.httpurlconnection;import Java.net.url;import Org.xmlpull.v1.xmlpullparser;import android.util.Xml;  public class Mobileservice {     public static string findaddress (String mobile) throws Exception {        inputstream is = MobileService.class.getClassLoader ()                 . getResourceAsStream ("Mobilesoap.xml");         byte[] data = Streamtool.readstream (IS);         string xml = new String (data, "UTF-8");         string soap = Xml.replaceall ("\ \ $mobile", mobile);         byte[] result = Soap.getbytes ("UTF-8");         string Path = "Http://webseRvice.webxml.com.cn/webservices/mobilecodews.asmx ";         url URL = new URL (path);         httpurlconnection conn = (httpurlconnection) Url.openconnection ();         conn.setrequestmethod ("POST");         conn.setdooutput (True);         conn.setconnecttimeout (5 *);          Conn.setrequestproperty ("Content-type",                  "Application/soap+xml; Charset=utf-8 ");         conn.setrequestproperty (" Content-Length ", String.valueof (Result.length));         outputstream OS = Conn.getoutputstream ();         os.write (Result);         os.flush ();         os.close ();          inputstream ISSOCP = Conn.getinputstream ();         return Parse (ISSOCP);     }     public static String Parse (InputStream is) throws Exception {        xmlpullparser parser = Xml.newpullparser ();         parser.setinput (IS, "UTF-8");         int event = Parser.geteventtype ();         while (Event! = xmlpullparser.end_document) {             switch (event) {            case xmlpullparser.start_tag:                 if ("Getmobilecodeinforesult". Equals (Parser.getname ())) {                     return Parser.nexttext ();                 }                 break;            }             event = Parser.next ();         }        return null;    }}





Package Cn.mzba.service; import Java.io.bytearrayoutputstream;import Java.io.inputstream; public class Streamtool {    /**     * read input stream data      *       * @param is     * @return       * @throws exception     */     public Static byte[] Readstream (InputStream is) throws Exception {        bytearrayoutputstream OS = new Bytearrayoutputstream ();         byte[] buffer = new byte[2048];         int len = 0;        while (len = is.read (buffer))! =-1) {            os.write (buffer, 0, Len);         }      &nbsP; is.close ();         return Os.tobytearray ();     }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.