📜  获取 post java html (1)

📅  最后修改于: 2023-12-03 14:57:13.009000             🧑  作者: Mango

获取Post数据的Java方法

在Web应用程序中,我们通常需要从HTML表单中获取用户提交的POST数据。接下来,我们将介绍如何使用Java代码来获取POST数据。

实现HttpGet方法

首先,我们需要引入Java中的Apache HttpClient库,可以使用以下Maven依赖项:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.13</version>
</dependency>

接着,我们可以使用HttpPost类来实现HttpPost方法,它允许我们向服务器发送POST请求,并获取服务器的响应数据。

以下是一个基本的HttpPost方法实现:

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpPostExample {

  public static void main(String[] args) throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost("http://localhost:8080/api");
    StringEntity requestEntity = new StringEntity(
            "{\"name\":\"John\", \"age\":\"30\"}",
            ContentType.APPLICATION_JSON);
    httpPost.setEntity(requestEntity);
    CloseableHttpResponse response = httpclient.execute(httpPost);
    try {
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            System.out.println(EntityUtils.toString(entity));
        }
    } finally {
        response.close();
    }
  }
}

以上代码将向http://localhost:8080/api发送一个POST请求,并将{\"name\":\"John\", \"age\":\"30\"}作为请求正文。

实现HttpServletRequest方法

您也可以使用Java Servlet的HttpServletRequest来获取POST数据,以下是使用该方法的示例:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet {

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
    Enumeration<String> paramNames = request.getParameterNames();
    while (paramNames.hasMoreElements()) {
      String paramName = paramNames.nextElement();
      String paramValue = request.getParameter(paramName);
      System.out.println(paramName + ": " + paramValue);
    }
    BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
    String inputLine;
    StringBuffer requestBody = new StringBuffer();
    while ((inputLine = reader.readLine()) != null) {
      requestBody.append(inputLine);
    }
    System.out.println(requestBody.toString());
  }
}

以上代码将在POST请求时打印出表单参数和请求正文。

简单粗暴的获取POST数据方法

最后,以下是一个简单粗暴的方法,可以将POST请求正文读取到字符串中:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PostData {
  public static String getPostData(HttpServletRequest req) throws IOException {
    StringBuilder sb = new StringBuilder();
    BufferedReader reader = null;
    try {
      reader = new BufferedReader(new InputStreamReader(req.getInputStream()));
      String line;
      while ((line = reader.readLine()) != null) {
        sb.append(line).append("\n");
      }
    } finally {
      if (reader != null) {
        reader.close();
      }
    }
    return sb.toString();
  }
}

以上代码将正文读取到了字符串sb中,然后返回字符串。注意这只是一个简单粗暴的实现方式,并不是最优秀的实现方式。

结束

以上是几种常见的方式来获取POST数据的Java方法的介绍。在实际应用中,您可以根据自己的需求选择最适合您的方法。