服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - Java如何实现URL带请求参数(get/post)及得到get和post请求url和参数列表的方法

Java如何实现URL带请求参数(get/post)及得到get和post请求url和参数列表的方法

2020-01-12 14:48mrr JAVA教程

本文给大家介绍Java如何实现URL带请求参数(get/post)及得到get和post请求url和参数列表的方法,涉及到 java获取post请求参数的方法,感兴趣的朋友一起看看吧

具体代码如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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()来得到请求完整路径

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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 请求参数的相关方法有所帮助,感谢大家一直以来对服务器之家网站的支持,有你们的支持,我们会做的更好。

延伸 · 阅读

精彩推荐