Java Call WebServiceURL

Source: Internet
Author: User
Tags readline

Code:

 packagecom.test;Importjava.io.BufferedReader;Importjava.io.DataOutputStream;Importjava.io.IOException;Importjava.io.InputStreamReader;Importjava.net.HttpURLConnection;Importjava.net.URL;Importjava.util.HashMap;Importjava.util.Map;Importnet.sf.json.JSONObject;/*** Java Access WebServiceURL*/ public classwebserviceurlaccess {//Save the returned cookie after login for use on next connection    PrivateString responsecookie; /*** Login to remote WebServiceURL based on username and password*/     publicString Login (string url, string Jsonparam) {//Declare return resultsString result = ""; BufferedReader Reader=NULL; DataOutputStream out=NULL; Try{URL PostURL=NewURL (url); HttpURLConnection Connection=(httpurlconnection) posturl.openconnection (); Connection.setdooutput (true); Connection.setdoinput (true); Connection.setrequestmethod ("POST");//Set Access typeConnection.setusecaches (false); Connection.setinstancefollowredirects (true); Connection.setrequestproperty ("content-type", "application/json");//Setting parameter TypesConnection.setrequestproperty ("connection", "keep-alive");            Connection.connect (); out=NewDataOutputStream (connection.getoutputstream ());            Out.writebytes (jsonparam);            Out.flush ();            Out.close (); Reader=NewBufferedReader (NewInputStreamReader (connection.getinputstream (), "UTF-8"));//set the return data encodingResponsecookie = Connection.getheaderfield ("set-cookie");//access to the cookie used//Read return resultsString line;  while(line = Reader.readline ())! =NULL) {result+=line ;            } reader.close ();        Connection.disconnect (); } Catch(ioexception E) {e.printstacktrace (); } finally {            Try {                if(out! =NULL) {out.close (); }            } Catch(Exception E) {e.printstacktrace (); }            Try {                if(reader! =NULL) {reader.close (); }            } Catch(Exception E) {e.printstacktrace (); }        }        returnresult; }    /*** Query operation after logging in to remote WebServiceURL*/     publicstring Searchdata (string url, string Jsonparam) {//Declare return resultsString result = ""; BufferedReader Reader=NULL; DataOutputStream out=NULL; Try{URL PostURL=NewURL (url); HttpURLConnection Connection=(httpurlconnection) posturl.openconnection (); Connection.setrequestproperty ("Cookie", responsecookie);//set the cookie returned at loginConnection.setdooutput (true); Connection.setdoinput (true); Connection.setrequestmethod ("POST"); Connection.setusecaches (false); Connection.setinstancefollowredirects (true); Connection.setrequestproperty ("content-type", "application/json");            Connection.connect (); out=NewDataOutputStream (connection.getoutputstream ());            Out.writebytes (jsonparam);            Out.flush ();            Out.close (); Reader=NewBufferedReader (NewInputStreamReader (connection.getinputstream (), "UTF-8"));//set the return data encodingString line;  while(line = Reader.readline ())! =NULL) {result+=line ;            } reader.close ();        Connection.disconnect (); } Catch(ioexception E) {e.printstacktrace (); } finally {            Try {                if(out! =NULL) {out.close (); }            } Catch(Exception E) {e.printstacktrace (); }            Try {                if(reader! =NULL) {reader.close (); }            } Catch(Exception E) {e.printstacktrace (); }        }        returnresult; }    /*** Use Example*/     public Static voidmain (string[] Args) {webserviceurlaccess access=Newwebserviceurlaccess (); //LoginString loginurl = "webserviceurl address"; Map<string, object> Loginparam =Newhashmap<string, object>(); Loginparam.put ("user", "login name");//depends on the actual parameters required by the Interface.Loginparam.put ("password", "login password");//depends on the actual parameters required by the Interface.String Loginjsonparam =jsonobject.fromobject (loginparam). toString (); String Resultlogin=Access.login (loginurl, loginjsonparam); //SearchdataString Searchurl = "webserviceurl address"; Map<string, object> Searchparam =Newhashmap<string, object>(); Searchparam.put ("fileds", "attribute name");//depends on the actual parameters required by the Interface.Searchparam.put ("date", "dates");//depends on the actual parameters required by the Interface.String Searchjsonparam =jsonobject.fromobject (searchparam). toString (); String Resultsearch=Access.searchdata (searchurl, searchjsonparam); }}

Java Call WebServiceURL

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.