Java:HttpURLConnection发送GET和POST请求

简介: Java:HttpURLConnection发送GET和POST请求

发送GET请求


package demo;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpDemo {
    public static void main(String[] args) throws IOException {
        String url = "https://www.baidu.com/";
        // 得到connection对象
        URL httpUrl = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) httpUrl.openConnection();
        //连接
        connection.connect();
        // 获取状态码 响应结果
        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            InputStream inputStream = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line = null;
            StringBuffer buffer = new StringBuffer();
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
            reader.close();
            System.out.println(buffer.toString());
        }
        // 断开连接
        connection.disconnect();
    }
}

发送POST请求


package demo;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpDemo {
    public static void main(String[] args) throws IOException {
        String url = "http://httpbin.org/post";
        //得到connection对象
        URL httpUrl = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) httpUrl.openConnection();
        //设置请求方式
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        // 设置请求头
        connection.setRequestProperty("Accept", "*/*");
        // 设置请求体
        String body = "name=Tom&age=23";
        OutputStream outputStream = connection.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
        writer.write(body);
        writer.close();
        //连接
        connection.connect();
        // 获取状态码 响应结果
        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            InputStream inputStream = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line = null;
            StringBuffer buffer = new StringBuffer();
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
            reader.close();
            System.out.println(buffer.toString());
        }
        // 断开连接
        connection.disconnect();
    }
}
相关文章
|
26天前
|
安全 算法 网络安全
高防CDN可以防御DDOS攻击吗
总结起来说,在面对日益严重的网络安全威胁时,高防CDN是一个非常有效且必要的工具。它不仅可以提升网站访问速度、改善用户体验,并且还能有效地抵御DDoS等多种形式网络攻击。
405 13
|
SQL 分布式计算 Serverless
EMR Serverless Spark:一站式全托管湖仓分析利器
本文根据2024云栖大会阿里云 EMR 团队负责人李钰(绝顶) 演讲实录整理而成
720 58
|
11月前
|
数据可视化 项目管理
解读:项目管理中的6大变革模型是什么?怎么用?
本文介绍了6种项目变革管理模型,包括莱温的三步变革模型和科特的八步变革模型。莱温模型适用于重大组织变革和引入新系统或技术,科特模型适用于需要长期适应变革的场合。
303 0
解读:项目管理中的6大变革模型是什么?怎么用?
|
12月前
|
机器学习/深度学习 算法 Python
基于BP神经网络的金融序列预测matlab仿真
本项目基于BP神经网络实现金融序列预测,使用MATLAB2022A版本进行开发与测试。通过构建多层前馈神经网络模型,利用历史金融数据训练模型,实现对未来金融时间序列如股票价格、汇率等的预测,并展示了预测误差及训练曲线。
212 12
|
弹性计算 固态存储 大数据
阿里云服务器多少钱一年?2024年阿里云服务器价格表曝光!
2024年最新阿里云服务器租用费用优惠价格表,轻量2核2G3M带宽轻量服务器一年82元,折合6.8元1个月,新老用户同享99元一年服务器,2核4G5M服务器ECS优惠价199元一年,2核4G4M轻量服务器298元一年,2核4G服务器30元3个月,4核16G10M服务器26元1个月、149元半年,8核32G服务器90元1个月、271元3个月,阿小云整理阿里云服务器租用费用价格表,包括一年优惠价格、一个月和1小时收费明细表
1470 3
|
存储 前端开发 API
[译]Django项目最常用的20个包
[译]Django项目最常用的20个包
477 1
|
Web App开发 存储 算法
|
Java Maven Spring
【十七】搭建SpringCloud项目一(Eureka)
【十七】搭建SpringCloud项目一(Eureka)
260 0
|
消息中间件 BI C#
C#中常见的winform控件命名规范
C#中常见的winform控件命名规范
660 0
|
前端开发 Java 数据库
SpringBoot项目复盘
SpringBoot项目复盘
94 0

热门文章

最新文章