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()方法来执行请求并获取响应。然后我们打印了响应的状态码和响应体。

相关推荐
深蓝电商API13 小时前
Scrapy+Rredis实现分布式爬虫入门与优化
分布式·爬虫·scrapy
sugar椰子皮2 天前
【web补环境篇-0】document.all
爬虫
interception2 天前
js逆向之京东原型链补环境h5st
javascript·爬虫·网络爬虫
半路_出家ren2 天前
17.python爬虫基础,基于正则表达式的爬虫,基于BeautifulSoup的爬虫
网络·爬虫·python·网络协议·正则表达式·网络爬虫·beautifulsoup
我想吃烤肉肉2 天前
Playwright中page.locator和Selenium中find_element区别
爬虫·python·测试工具·自动化
lbb 小魔仙3 天前
【Python】零基础学 Python 爬虫:从原理到反爬,构建企业级爬虫系统
开发语言·爬虫·python
努力变大白3 天前
借助AI零基础快速学会Python爬取网页信息-以天眼查爬虫为例
人工智能·爬虫·python
AC赳赳老秦3 天前
Unity游戏开发实战指南:核心逻辑与场景构建详解
开发语言·spring boot·爬虫·搜索引擎·全文检索·lucene·deepseek