使用java.net.URLConnection发起get或post请求的解决方案分享
下文笔者讲述URLConnection进行get和post请求的方法分享,如下所示、
GET和POST区别说明(2023最新面经)
get和post请求简介
get和post是http请求的两种不同方式 两种方式的区别在于:请求可发送数据的方式不同
URLConnectio发送Get和Post请求的示例
//Get请求
URLConnection connection = new URL(url + "?" + query).openConnection();
connection.setRequestProperty("Accept-Charset", charset);
InputStream response = connection.getInputStream();
//Post请求
URLConnection connection = new URL(url).openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
try (OutputStream output = connection.getOutputStream()) {
output.write(query.getBytes(charset));
}
InputStream response = connection.getInputStream();
相关阅读:GET和POST区别说明(2023最新面经)
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


