httpclient发送post请求

迈不过友情╰ 2023-10-08 20:16 144阅读 0赞

使用Apache HttpComponents提供的HttpClient发送POST请求可以分为以下几个步骤:

创建 HttpClient 对象

  1. CloseableHttpClient httpClient = HttpClients.createDefault();

创建 HttpPost 对象并设置请求 URL

  1. HttpPost httpPost = new HttpPost("http://www.example.com/submit-form");

设置 POST 请求的参数,可以使用UrlEncodedFormEntity对Form表单进行编码

  1. List<NameValuePair> formParams = new ArrayList<>();
  2. formParams.add(new BasicNameValuePair("username", "user123"));
  3. formParams.add(new BasicNameValuePair("password", "pass123"));
  4. UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(formParams, Consts.UTF_8);
  5. httpPost.setEntity(urlEncodedFormEntity);

执行 HTTP POST 请求

  1. CloseableHttpResponse httpResponse = httpClient.execute(httpPost);

处理结果

  1. HttpEntity responseEntity = httpResponse.getEntity();
  2. if (responseEntity != null) {
  3. // 使用 EntityUtils 将响应实体转换为字符串
  4. String responseString = EntityUtils.toString(responseEntity);
  5. // 对响应结果进行处理
  6. System.out.println(responseString);
  7. }

关闭资源

  1. httpResponse.close();
  2. httpClient.close();

完整示例代码:

  1. import java.io.IOException;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.apache.http.Consts;
  5. import org.apache.http.HttpEntity;
  6. import org.apache.http.NameValuePair;
  7. import org.apache.http.client.methods.CloseableHttpResponse;
  8. import org.apache.http.client.methods.HttpPost;
  9. import org.apache.http.client.utils.URIBuilder;
  10. import org.apache.http.entity.StringEntity;
  11. import org.apache.http.message.BasicNameValuePair;
  12. import org.apache.http.impl.client.CloseableHttpClient;
  13. import org.apache.http.impl.client.HttpClients;
  14. import org.apache.http.client.entity.UrlEncodedFormEntity;
  15. import org.apache.http.util.EntityUtils;
  16. public class HttpClientExample {
  17. public static void main(String[] args) throws IOException {
  18. CloseableHttpClient httpClient = HttpClients.createDefault();
  19. try {
  20. HttpPost httpPost = new HttpPost("http://www.example.com/submit-form");
  21. List<NameValuePair> formParams = new ArrayList<>();
  22. formParams.add(new BasicNameValuePair("username", "user123"));
  23. formParams.add(new BasicNameValuePair("password", "pass123"));
  24. UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(formParams, Consts.UTF_8);
  25. httpPost.setEntity(urlEncodedFormEntity);
  26. CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
  27. try {
  28. HttpEntity responseEntity = httpResponse.getEntity();
  29. if (responseEntity != null) {
  30. String responseString = EntityUtils.toString(responseEntity);
  31. System.out.println(responseString);
  32. }
  33. } finally {
  34. httpResponse.close();
  35. }
  36. } finally {
  37. httpClient.close();
  38. }
  39. }
  40. }

以上是使用 HttpClient 发送 POST 请求的方式。需要注意的是,其它类型的 HTTP 方法同样可以通过 HttpClient 来实现,只需使用对应的 HttpGet、HttpPut、HttpDelete等类即可。

发表评论

表情:
评论列表 (有 0 条评论,144人围观)

还没有评论,来说两句吧...

相关阅读