Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of get and post requests of jdk

Shulou Source: shulou.com Published: 2022-06-03 11:27:14 10月18日 Update

This article introduces the sample analysis of jdk's get and post requests, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.

Post OutputStreamWriter out = null; HttpURLConnection conn = null; BufferedReader reader = null; StringBuilder response = new StringBuilder (); URL httpUrl = null; try {httpUrl = new URL ("http://www.baidu.com"); / / establish a connection conn = (HttpURLConnection) httpUrl.openConnection (); conn.setRequestMethod (" POST ") Conn.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded"); conn.setUseCaches (false); / / set not to cache conn.setInstanceFollowRedirects (true); conn.setDoOutput (true); conn.setDoInput (true); conn.connect (); out = new OutputStreamWriter (conn.getOutputStream ()) Out.write (params); out.flush (); / / read response reader = new BufferedReader (new InputStreamReader (conn.getInputStream (); String lines; while ((lines = reader.readLine ())! = null) {lines = new String (lines.getBytes (), "utf-8") Response.append (lines);} System.out.println (response.toString ());} catch (Exception e) {e.printStackTrace ();} get/** * send GET request * * @ param url destination address * @ param parameters request parameter, Map type. * @ return remote response result * / public static String sendGet (String url, Map parameters) {StringBuilder result = new StringBuilder (); BufferedReader in = null;// read response input stream StringBuilder sbParams = new StringBuilder (); / / Storage parameter String params = "" / / Encoding request parameter try {/ / Encoding request parameter for (String name: parameters.keySet ()) {sbParams.append (name) .append ("=") .append (java.net.URLEncoder.encode (parameters.get (name), "UTF-8") .append ("&");} String requestParam = sbParams.toString () Params = requestParam.substring (0, requestParam.length ()-1); String fullUrl = url + "?" + params; logger.info ("get request:" + fullUrl); / / create URL object URL connURL = new URL (fullUrl); / / Open URL connection HttpURLConnection httpConn = (HttpURLConnection) connURL.openConnection () / / set common properties httpConn.setRequestProperty ("Accept", "* / *"); httpConn.setRequestProperty ("Connection", "Keep-Alive"); httpConn.setRequestProperty ("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); / / establish the actual connection httpConn.connect () / / Map headers = httpConn.getHeaderFields (); / / define the BufferedReader input stream to read the response of URL, and set the encoding mode in = new BufferedReader (new InputStreamReader (httpConn.getInputStream (), StandardCharsets.UTF_8)); String line / / read the returned content while ((line = in.readLine ())! = null) {result.append (line);} logger.info ("get request result:" + result.toString ());} catch (Exception e) {e.printStackTrace () } finally {try {if (in! = null) {in.close ();}} catch (IOException ex) {ex.printStackTrace ();}} return result.toString () } this is the end of the sample analysis of jdk's get and post requests. I hope the above can be of some help and learn more. If you think the article is good, you can share it for more people to see.

Tags: Parameters content coding examples analysis more results help input good interest address actual object partner attribute article way purpose Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi NVidia OPPO Reno Microsoft MySQL