时间:2021-05-20
具体代码如下所示:
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; }下面给大家介绍java得到GET和POST请求URL和参数列表的方法
在servlet中GET请求可以通过HttpServletRequest的getRequestURL方法和getQueryString()得到完整的请求路径和请求所有参数列表,POST的需要getParameterMap()方法遍历得到,不论GET或POST都可以通过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 + "&"; } } // 去掉最后一个空格 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 + "&"; } } // 去掉最后一个空格 queryString = queryString.substring(0, queryString.length() - 1); writer.println("POST " + request.getRequestURL() + " " + queryString); } }以上代码简单易懂,希望对大家学习 java post get url 请求参数的相关方法有所帮助,感谢大家一直以来对网站的支持,有你们的支持,我们会做的更好。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
get请求和post请求的区别有: 1、GET通常把参数包含在URL中,而POST一般通过requestbody来传递参数。且GET产生的URL地址可以被标记
前言目前我们的项目中仅使用到GET和POST两种请求方式,对于GET请求,请求的参数会拼接在Url中;对于POST请求来说,我们可以通过Body或表单来提交一些
请求方式,分为GET与POST:GET最为常见的HTTP请求,普通上网浏览页面就是GET。GET方式的参数请求直接跟在URL后,以问号开始。(JS中用windo
请求方式,分为GET与POST:GET最为常见的HTTP请求,普通上网浏览页面就是GET。GET方式的参数请求直接跟在URL后,以问号开始。(JS中用windo
react中的ajax封装实例详解代码块**opts:{'可选参数'}**method:请求方式:GET/POST,默认值:'GET';**url:发送请求的地