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