Recently docking a WebService interface to fetch data, from the online mixed code to find a method, the specific author has not been able to remember who is, now put the code out, hope can help everyone, code as follows, simple rough
PublicString Getwebservice () {httpurlconnection Connection=NULL; OutputStream OS=NULL; intResponsecode = 0; StringBuilder SB=NewStringBuilder (); //First step: Create a service address, not a WSDL addressURL url =NULL; Try{URL=NewURL (""); Fill in the URL address here}Catch(malformedurlexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } //Step Two: Open a connection to the service address Try{Connection=(HttpURLConnection) url.openconnection (); //Step Three: Set the parameters//3.1 Send mode settings: Post must be uppercaseConnection.setrequestmethod ("POST"); //3.2 Setting Data format: Content-typeConnection.setrequestproperty ("Content-type", "Text/xml;charset=utf-8"); //3.3 Set the input and output, because the default newly created connection does not have read and write permissions,Connection.setdoinput (true); Connection.setdooutput (true); OS=Connection.getoutputstream (); //Fifth Step: Receive service-side response, printResponsecode =Connection.getresponsecode (); String Temp=NULL; if(= = Responsecode) {//indicates a successful service-side responseInputStream is =Connection.getinputstream (); InputStreamReader ISR=NewInputStreamReader (IS); BufferedReader BR=NewBufferedReader (ISR); while(NULL! = (temp =Br.readline ())) {sb.append (temp); } System.out.println (Sb.tostring ()); Is.close (); Isr.close (); Br.close (); } os.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } returnsb.tostring (); }
Java Request Webservce Interface