1. Save Cookies at Login
1 Private String Responsecookie;
View Code
1 /**2 * Retrieve the returned data based on the URL address and parameters3 */4 PrivateString Login_postmethod (string url, string jsonparam)throwsIOException {5URL PostURL =Newurl (url);6HttpURLConnection connection =(HttpURLConnection) posturl.openconnection ();7Connection.setdooutput (true);8Connection.setdoinput (true);9Connection.setrequestmethod ("POST");//Set Access typeTenConnection.setusecaches (false); OneConnection.setinstancefollowredirects (true); AConnection.setrequestproperty ("Content-type", "Application/json");//Setting parameter Types -Connection.setrequestproperty ("Connection", "keep-alive"); - Connection.connect (); the -DataOutputStream out =NewDataOutputStream (Connection.getoutputstream ()); - out.writebytes (jsonparam); - Out.flush (); + out.close (); - +BufferedReader reader =NewBufferedReader (NewInputStreamReader (Connection.getinputstream (), "UTF-8"));//set the return data encoding AResponsecookie = Connection.getheaderfield ("Set-cookie");//access to the cookie used at - //GET request return result -String result = ""; - String Line; - while(line = Reader.readline ())! =NULL) { -Result + =Line ; in } - to reader.close (); + Connection.disconnect (); - theLogger.info ("URL:" +URL); * logger.info (result); $ Panax Notoginseng returnresult; -}View Code
2. Access the WebService interface again, based on the cookie stored for the first login
1 /**2 * Retrieve the returned data based on the URL address and parameters3 */4 PrivateString Reconnnect_postmethod (string url, string jsonparam)throwsIOException {5URL PostURL =Newurl (url);6HttpURLConnection connection =(HttpURLConnection) posturl.openconnection ();7Connection.setrequestproperty ("Cookie", Responsecookie);//set the cookie returned at login8Connection.setdooutput (true);9Connection.setdoinput (true);TenConnection.setrequestmethod ("POST"); OneConnection.setusecaches (false); AConnection.setinstancefollowredirects (true); -Connection.setrequestproperty ("Content-type", "Application/json"); - Connection.connect (); the -DataOutputStream out =NewDataOutputStream (Connection.getoutputstream ()); - out.writebytes (jsonparam); - Out.flush (); + out.close (); - +BufferedReader reader =NewBufferedReader (NewInputStreamReader (Connection.getinputstream (), "UTF-8")); A at //GET request return result -String result = ""; - String Line; - while(line = Reader.readline ())! =NULL) { -Result + =Line ; - } in - reader.close (); to Connection.disconnect (); + - returnresult; the}View Code
Java Access Webservcie URL