How does Java implement URL with request parameter of get and post and get and post request URL and parameter list method

  • 2020-04-01 04:19:39
  • OfStack

The specific code is as follows:


public static String sendGet(String url,String param)
 {
 String result = "";
 try{
 String urlName = url + "?"+param;//
 URL U = new URL(urlName);
 URLConnection connection = U.openConnection();
 connection.connect();
 BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
 String line;
 while ((line = in.readLine())!= null)
 {
 result += line;
 }
 in.close(); 
 }catch(Exception e){
 System.out.println("Helloword !!!!! "+e);
 }
 return result;
 }
 public static String sendPost(String url,String param)
 {
 String result="";
 try{
 URL httpurl = new URL(url);
 HttpURLConnection httpConn = (HttpURLConnection)httpurl.openConnection();  
 httpConn.setDoOutput(true);
 httpConn.setDoInput(true);
 PrintWriter out = new PrintWriter(httpConn.getOutputStream());
 out.print(param);
 out.flush();
 out.close();
 BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
 String line;
 while ((line = in.readLine())!= null)
 {
 result += line; 
 }
 in.close();
 }catch(Exception e){
 System.out.println("Helloword ! "+e);
 }
 return result;
 }

Here's a guide   Java methods to GET GET and POST request urls and parameter lists

  In the servlet, GET requests can be obtained through the getRequestURL method of HttpServletRequest and getQueryString() to GET the full request path and the list of all parameters of the request, POST needs the getParameterMap() method to be traversed, and both GET and POST can GET the full request path through getRequestURL+getParameterMap()


package com.zuidaima       
import java.io.IOException; 
import java.io.PrintWriter; 
import java.util.Map; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
public class GetParams extends HttpServlet { 
 private static final long serialVersionUID = 1L; 
 public GetParams() { 
  super(); 
 } 
 protected void doGet(HttpServletRequest request, 
   HttpServletResponse response) throws ServletException, IOException { 
  PrintWriter writer = response.getWriter(); 
  writer.println("GET " + request.getRequestURL() + " " 
    + request.getQueryString()); 
  Map<String, String[]> params = request.getParameterMap(); 
  String queryString = ""; 
  for (String key : params.keySet()) { 
   String[] values = params.get(key); 
   for (int i = 0; i < values.length; i++) { 
    String value = values[i]; 
    queryString += key + "=" + value + "&"; 
   } 
  } 
  //Remove the last space
  queryString = queryString.substring(0, queryString.length() - 1); 
  writer.println("GET " + request.getRequestURL() + " " + queryString); 
 } 
 protected void doPost(HttpServletRequest request, 
   HttpServletResponse response) throws ServletException, IOException { 
  PrintWriter writer = response.getWriter(); 
  Map<String, String[]> params = request.getParameterMap(); 
  String queryString = ""; 
  for (String key : params.keySet()) { 
   String[] values = params.get(key); 
   for (int i = 0; i < values.length; i++) { 
    String value = values[i]; 
    queryString += key + "=" + value + "&"; 
   } 
  } 
  //Remove the last space
  queryString = queryString.substring(0, queryString.length() - 1); 
  writer.println("POST " + request.getRequestURL() + " " + queryString); 
 } 
}

Related articles: