首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

在Java中发送HTTP POST请求

在Java中发送HTTP POST请求,可以使用Java的内置库java.net.HttpURLConnection或者使用第三方库如Apache HttpClient。

使用java.net.HttpURLConnection发送HTTP POST请求的示例代码如下:

代码语言:java
复制
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class HttpPostExample {
    public static void main(String[] args) throws Exception {
        String url = "https://example.com/api";
        String data = "key1=value1&key2=value2";

        URL objUrl = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) objUrl.openConnection();

        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.setRequestProperty("Content-Length", Integer.toString(data.getBytes(StandardCharsets.UTF_8).length));

        connection.setDoOutput(true);
        OutputStream outputStream = connection.getOutputStream();
        outputStream.write(data.getBytes(StandardCharsets.UTF_8));
        outputStream.flush();
        outputStream.close();

        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);

        connection.disconnect();
    }
}

使用Apache HttpClient发送HTTP POST请求的示例代码如下:

代码语言:java
复制
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 {
        String url = "https://example.com/api";
        String data = "key1=value1&key2=value2";

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");

        StringEntity stringEntity = new StringEntity(data);
        httpPost.setEntity(stringEntity);

        CloseableHttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        String responseString = EntityUtils.toString(entity, StandardCharsets.UTF_8);
        EntityUtils.consume(entity);

        System.out.println("Response: " + responseString);
        response.close();
        httpClient.close();
    }
}

这两个示例代码都可以发送HTTP POST请求,并且可以根据需要修改请求头、请求体等信息。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

1分12秒

使用requests库来发送HTTP请求

6分42秒

12.尚硅谷_AJAX-AJAX发送POST请求

1分16秒

使用 request 和 cheerio 库来发送 HTTP 请求

18分10秒

8. 尚硅谷_佟刚_JavaWEB_HTTP 协议_GET&POST请求.wmv

10分37秒

06-Servlet-1/17-尚硅谷-Servlet-POST请求HTTP协议内容介绍

19分16秒

Python爬虫项目实战 5 requests中的post请求 学习猿地

31分16秒

10.使用 Utils 在列表中请求图片.avi

11分37秒

107.使用Image-Loader在ListView中请求图片.avi

22分4秒

87.使用Volley在ListView或者GridView中请求图片.avi

9分47秒

09_尚硅谷_SSM面试题_SpringMVC中如何解决POST请求中文乱码问....avi

33分30秒

Java零基础-299-多态在开发中的作用

9分4秒

Java零基础-300-总结多态在开发中的作用

领券