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();

}
}


            </div>
目录
相关文章
|
Rust API 开发者
【一起学Rust | 框架篇 | ws-rs框架】属于Rust的Websocket框架——ws-rs
【一起学Rust | 框架篇 | ws-rs框架】属于Rust的Websocket框架——ws-rs
1635 0
|
Python Windows
六、【windows】更改 Python 的 pip install 默认安装依赖路径,及cmd下pip安装成功的包,pycharm却找不到
六、【windows】更改 Python 的 pip install 默认安装依赖路径,及cmd下pip安装成功的包,pycharm却找不到
3120 0
六、【windows】更改 Python 的 pip install 默认安装依赖路径,及cmd下pip安装成功的包,pycharm却找不到
|
人工智能 API Python
【AI大模型应用开发】1.1 Prompt Engineering(提示词工程)- 用OpenAI API实战,优化方法论总结
【AI大模型应用开发】1.1 Prompt Engineering(提示词工程)- 用OpenAI API实战,优化方法论总结
1074 0
|
算法 Java 数据安全/隐私保护
java实现微信公众号token验证
java实现微信公众号token验证
java实现微信公众号token验证
|
开发者
手把手教你微信公众号如何给指定用户发送消息提醒
消息提醒功能是提升用户满意度的最有效方式,基于微信聊天的消息提醒也是现在最常见的消息提醒方式之一,
手把手教你微信公众号如何给指定用户发送消息提醒
|
关系型数据库 MySQL 数据库
|
Java Android开发 C++
安装完android studio,启动时碰到"failed to load jvm dll"的解决方案
安装完android studio,启动时碰到"failed to load jvm dll"的解决方案   安装Microsoft Visual C++ 2010 Redistributable Package 32 bit: http://www.
4232 0
|
4天前
|
数据采集 人工智能 安全