一个简单的访问http的java和python脚本

python脚本

import requests

# 发送 GET 请求
response = requests.get('https://www.baidu.com')

# 打印 HTTP 状态码
print("HTTP Status Code:", response.status_code)

# 打印响应内容
print("Response Content:", response.text)

java脚本

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class CC {
    public static void main(String[] args) {
        try {
            // 创建HttpClient实例
            HttpClient client = HttpClient.newHttpClient();

            // 创建HttpRequest实例,指定GET请求的URI
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create("https://example.com"))
                    .GET() // 默认就是GET方法,可以省略
                    .build();

            // 发送请求,并接收HttpResponse
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

            // 打印HTTP状态码
            System.out.println("HTTP Code: " + response.statusCode());

            // 打印响应内容
            System.out.println("Response Body: " + response.body());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

转载请备注引用地址:编程记忆 » 一个简单的访问http的java和python脚本