Java Access Webservcie URL

Source: Internet
Author: User

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

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.