WebService Get/post/soap Way Request

Source: Internet
Author: User
Tags soap

ImportJava.io.ByteArrayOutputStream;ImportJava.io.FileInputStream;ImportJava.io.InputStream;Importjava.net.HttpURLConnection;ImportJava.net.URL;Importorg.apache.commons.httpclient.HttpClient;ImportOrg.apache.commons.httpclient.methods.PostMethod;/**call the third-party WebService service to get the phone number information **/ Public classMobilecodeservice {//1. Http-get Mode Access WebService     Public voidGet (String Mobilecode, String UserID)throwsexception{URL url=NewURL ("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=" +mobilecode+ "&amp ; userid= "+UserID); HttpURLConnection Conn=(HttpURLConnection) url.openconnection (); Conn.setconnecttimeout (5000); Conn.setrequestmethod ("GET"); if(Conn.getresponsecode () ==HTTPURLCONNECTION.HTTP_OK) {//Result Code =200InputStream is=Conn.getinputstream (); //memory Stream,Bytearrayoutputstream boas=NewBytearrayoutputstream (); byte[] buffer=New byte[1024]; intLen=-1;  while(Len=is.read (buffer))!=-1) {boas.write (buffer,0, Len); } System.out.println ("GET Request data:" +boas.tostring ());            Boas.close ();        Is.close (); }    }        //2.Post Request: Simulating implementation of HTTP requests via the Http-client framework     Public voidPost (string mobilecode, String UserID)throwsexception{/**httpclient to access the network: * 1. Prepare a request client: Browser * 2. Prepare request method: Get, POST * 3. Set the parameter * 4 to be passed. Execution Request * 5. Get Results*/HttpClient Client=NewHttpClient (); Postmethod Postmethod=NewPostmethod ("Http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo"); //3. Set Request ParametersPostmethod.setparameter ("Mobilecode", Mobilecode); Postmethod.setparameter ("UserID", UserID); //4. Execution request, result code        intCode=Client.executemethod (Postmethod); //5. Get ResultsString result=postmethod.getresponsebodyasstring (); System.out.println ("The result of the POST request:" +result); }    //2.Post Request: Simulating implementation of HTTP requests via the Http-client framework     Public voidSoap ()throwsexception{HttpClient Client=NewHttpClient (); Postmethod Postmethod=NewPostmethod ("Http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx"); //3. Set Request ParametersPostmethod.setrequestbody (NewFileInputStream ("D:/soap.xml")); //Modify the requested headerPostmethod.setrequestheader ("Content-type", "text/xml; Charset=utf-8 "); //4. Execution request, result code        intCode=Client.executemethod (Postmethod); System.out.println ("Result code:" +code); //5. Get ResultsString result=postmethod.getresponsebodyasstring (); System.out.println ("The result of the POST request:" +result); }     Public Static voidMain (string[] args)throwsexception{mobilecodeservice ws=NewMobilecodeservice (); Ws.get ("15958083603", "" "); Ws.post ("15958012349", "" ");    Ws.soap (); }}

WebService Get/post/soap Way Request

Related Article

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.