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+ "& ; 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