OkHttpClient实例

以下是OkHttpClient的一个简单用法示例:

c 复制代码
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url("jshk.com.cn")
                .build();

        try {
            Response response = client.newCall(request).execute();
            System.out.println(response.code());
            System.out.println(response.body().string());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

在上面的示例中,我们创建了一个OkHttpClient实例,并使用它来发送一个简单的GET请求。我们构建了一个请求对象,并使用client.newCall(request).execute()方法来执行请求并获取响应。然后我们打印了响应的状态码和响应体。

相关推荐
IT毕设实战小研18 小时前
电影数据可视化推荐系统
大数据·后端·爬虫·python·算法·信息可视化·课程设计
笔触狂放21 小时前
第3章 抓取静态网页数据
爬虫·python
笔触狂放1 天前
第1章 认识网络爬虫
爬虫
深蓝电商API2 天前
Referer 校验原理
爬虫·referer
天空1102 天前
你的网站在 AI 眼里是什么样:三类「人能看见、AI 看不见」的内容
爬虫·seo
zx_741484812 天前
【Python入门】爬虫实战:Requests + XPath 从基础到实战
开发语言·爬虫·python
for_ever_love__2 天前
python爬虫: GET请求与POST请求
开发语言·爬虫·python·网络编程
IT毕设实战小研2 天前
基于安卓的考研资讯系统设计与实现
android·大数据·vue.js·爬虫·python·考研·课程设计
IT毕设实战小研2 天前
基于安卓的空气质量查询系统
android·大数据·vue.js·爬虫·python·django·课程设计
benchmark_cc3 天前
1000只ETF的5分钟K线如何批量获取?QuantDash分页策略与高性能Python实践
开发语言·人工智能·爬虫·python·算法·quantdash·量化数据源