In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "JAVA background how to achieve POST/GET access method", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "JAVA background how to achieve POST/GET access method" bar!
I. specific role
Invoke the service API provided by other vendors
2. Corresponding example 1. Getpublic static void main (String [] args) {String result = ""; BufferedReader in = null; try {String urlName = url; URL realUrl = new URL (urlName); URLConnection conn = realUrl.openConnection (); / / Open the connection to URL conn.setRequestProperty ("accept", "* / *"); / / set the general request attribute conn.setRequestProperty ("connection", "Keep-Alive") Conn.setRequestProperty ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); conn.setConnectTimeout (4000); conn.connect (); / / establish the actual connection in = new BufferedReader (new InputStreamReader (conn.getInputStream (), "UTF-8")); / / define the BufferedReader input stream to read the URL response String line While ((line = in.readLine ())! = null) {result + = line;}} catch (Exception e) {System.out.println ("there was an exception sending a GET request!" + e);} finally {/ / use the finally block to close the input stream try {if (in! = null) {in.close ();}} catch (IOException ex) {System.out.println ("close stream exception");}} System.out.println (result) 2. Postpublic static void main (String [] args) {PrintWriter out = null; BufferedReader in = null; String result = ""; try {URL realUrl = new URL (url); / / Open the connection to URL URLConnection conn = realUrl.openConnection (); / / set the general request property conn.setRequestProperty ("accept", "* / *") Conn.setRequestProperty ("connection", "Keep-Alive"); conn.setRequestProperty ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); / / the following two lines of conn.setDoOutput (true) and conn.setDoInput (true) must be set to send an POST request / / get the output stream corresponding to the URLConnection object out = new PrintWriter (conn.getOutputStream ()); / / send the request parameter out.print (param); / / buffer out.flush () of the flush output stream; / / define the BufferedReader input stream to read the URL response in = new BufferedReader (new InputStreamReader (conn.getInputStream (); String line While ((line = in.readLine ())! = null) {result + = line;}} catch (Exception e) {System.out.println ("[POST request] to address:" + url + "send data:" + param + "error!") } finally {/ / use finally blocks to close the output stream and input stream try {if (out! = null) {in.close ();} if (in! = null) {in.close ();}} catch (IOException ex) {System.out.println ("close stream exception") }} System.out.println (result);} Thank you for your reading. The above is the content of "how to implement POST/GET access method in JAVA backend". After the study of this article, I believe you have a deeper understanding of how to implement POST/GET access method in JAVA backend, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.