最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java使用okhttp3發(fā)送請(qǐng)求的實(shí)現(xiàn)示例

 更新時(shí)間:2025年12月23日 10:46:46   作者:moxiaoran5753  
OkHttp3是一個(gè)高效的HTTP客戶(hù)端,支持HTTP/2、GZIP壓縮、響應(yīng)緩存和異步/同步請(qǐng)求,本文就來(lái)介紹一下Java使用okhttp3發(fā)送請(qǐng)求的實(shí)現(xiàn)示例,感興趣的可以了解一下

一、OkHttp3 簡(jiǎn)介

OkHttp3 是一個(gè)高效的 HTTP 客戶(hù)端,由 Square 公司開(kāi)發(fā),具有以下核心特點(diǎn):

  • 連接池 - 減少請(qǐng)求延遲,支持HTTP/2和SPDY
  • 透明GZIP壓縮 - 自動(dòng)壓縮請(qǐng)求體,減少數(shù)據(jù)傳輸量
  • 響應(yīng)緩存 - 避免重復(fù)網(wǎng)絡(luò)請(qǐng)求
  • 自動(dòng)重試 - 處理瞬時(shí)故障和網(wǎng)絡(luò)問(wèn)題
  • 異步/同步支持 - 靈活的調(diào)用方式

二、使用示例

準(zhǔn)備工作

引入依賴(lài):

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.12.0</version>
</dependency>

創(chuàng)建OkHttpClient實(shí)例

import okhttp3.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class OkHttpExample {
    // 創(chuàng)建全局OkHttpClient實(shí)例
    private static final OkHttpClient client = new OkHttpClient.Builder()
            .connectTimeout(10, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .writeTimeout(30, TimeUnit.SECONDS)
            .addInterceptor(new LoggingInterceptor()) // 添加日志攔截器
            .build();
    
    // 簡(jiǎn)單的日志攔截器
    static class LoggingInterceptor implements Interceptor {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            long startTime = System.nanoTime();
            
            System.out.println(String.format("Sending request %s on %s%n%s",
                    request.url(), chain.connection(), request.headers()));
            
            Response response = chain.proceed(request);
            
            long endTime = System.nanoTime();
            System.out.println(String.format("Received response for %s in %.1fms%n%s",
                    response.request().url(), (endTime - startTime) / 1e6d, response.headers()));
            
            return response;
        }
    }
}

1.發(fā)送GET請(qǐng)求

下面的示例涵蓋普通get請(qǐng)求,帶查詢(xún)參數(shù)的get請(qǐng)求和帶請(qǐng)求頭的get請(qǐng)求。

public class GetRequestExample {
    
    public static void basicGetRequest() throws IOException {
        String url = "https://jsonplaceholder.typicode.com/posts/1";
        
        Request request = new Request.Builder()
                .url(url)
                .get()  // 顯式聲明GET方法,可選
                .build();
        
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code: " + response);
            }
            
            String responseBody = response.body().string();
            System.out.println("Response: " + responseBody);
        }
    }
    
    // 帶查詢(xún)參數(shù)的GET請(qǐng)求
    public static void getWithQueryParams() throws IOException {
        HttpUrl url = HttpUrl.parse("https://jsonplaceholder.typicode.com/posts")
                .newBuilder()
                .addQueryParameter("userId", "1")
                .addQueryParameter("_limit", "5")
                .build();
        
        Request request = new Request.Builder()
                .url(url)
                .build();
        
        try (Response response = client.newCall(request).execute()) {
            System.out.println("Response: " + response.body().string());
        }
    }
    
    // 帶請(qǐng)求頭的GET請(qǐng)求
    public static void getWithHeaders() throws IOException {
        Request request = new Request.Builder()
                .url("https://api.github.com/users/octocat")
                .addHeader("User-Agent", "OkHttp-Example")
                .addHeader("Accept", "application/vnd.github.v3+json")
                .addHeader("Authorization", "Bearer your-token-here")
                .build();
        
        try (Response response = client.newCall(request).execute()) {
            System.out.println("Response: " + response.body().string());
        }
    }
}

2. 發(fā)送POST請(qǐng)求

下面的示例涵蓋,發(fā)送json數(shù)據(jù),表單數(shù)據(jù)和文件上傳的post請(qǐng)求

public class PostRequestExample {
    
    // POST JSON數(shù)據(jù)
    public static void postJson() throws IOException {
        String json = "{\"title\":\"foo\",\"body\":\"bar\",\"userId\":1}";
        
        RequestBody body = RequestBody.create(
                json,
                MediaType.parse("application/json; charset=utf-8")
        );
        
        Request request = new Request.Builder()
                .url("https://jsonplaceholder.typicode.com/posts")
                .post(body)
                .addHeader("Content-Type", "application/json")
                .build();
        
        try (Response response = client.newCall(request).execute()) {
            System.out.println("Response: " + response.body().string());
        }
    }
    
    // 發(fā)送表單數(shù)據(jù)
    public static void postForm() throws IOException {
        RequestBody formBody = new FormBody.Builder()
                .add("username", "john_doe")
                .add("password", "secret123")
                .add("grant_type", "password")
                .build();
        
        Request request = new Request.Builder()
                .url("https://httpbin.org/post")
                .post(formBody)
                .build();
        
        try (Response response = client.newCall(request).execute()) {
            System.out.println("Response: " + response.body().string());
        }
    }
    
    // 發(fā)送multipart表單(文件上傳)
    public static void postMultipart() throws IOException {
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("title", "My File")
                .addFormDataPart("file", "filename.txt",
                        RequestBody.create("file content", MediaType.parse("text/plain")))
                .addFormDataPart("image", "image.jpg",
                        RequestBody.create(new byte[]{/* 圖片數(shù)據(jù) */}, MediaType.parse("image/jpeg")))
                .build();
        
        Request request = new Request.Builder()
                .url("https://httpbin.org/post")
                .post(requestBody)
                .build();
        
        try (Response response = client.newCall(request).execute()) {
            System.out.println("Response: " + response.body().string());
        }
    }
}

3.發(fā)送PUT請(qǐng)求

    public static void putRequest() throws IOException {
        String json = "{\"id\":1,\"title\":\"updated title\",\"body\":\"updated body\",\"userId\":1}";
        
        RequestBody body = RequestBody.create(
                json,
                MediaType.parse("application/json; charset=utf-8")
        );
        
        Request request = new Request.Builder()
                .url("https://jsonplaceholder.typicode.com/posts/1")
                .put(body)
                .build();
        
        try (Response response = client.newCall(request).execute()) {
            System.out.println("PUT Response: " + response.body().string());
        }
    }

4.發(fā)送PATCH請(qǐng)求

PATCH 請(qǐng)求用于對(duì)資源進(jìn)行部分更新,與 PUT(全量更新)不同,PATCH 只更新提供的字段。

public static void patchRequest() throws IOException {
        String json = "{\"title\":\"patched title\"}";
        
        RequestBody body = RequestBody.create(
                json,
                MediaType.parse("application/json; charset=utf-8")
        );
        
        Request request = new Request.Builder()
                .url("https://jsonplaceholder.typicode.com/posts/1")
                .patch(body)
                .build();
        
        try (Response response = client.newCall(request).execute()) {
            System.out.println("PATCH Response: " + response.body().string());
        }
    }

5.發(fā)送DELETE請(qǐng)求

 public static void deleteRequest() throws IOException {
        Request request = new Request.Builder()
                .url("https://jsonplaceholder.typicode.com/posts/1")
                .delete()
                .build();
        
        try (Response response = client.newCall(request).execute()) {
            System.out.println("DELETE Response: " + response.body().string());
        }
    }

6.發(fā)送異步請(qǐng)求

public class AsyncRequestExample {
    
    public static void asyncGetRequest() {
        Request request = new Request.Builder()
                .url("https://jsonplaceholder.typicode.com/posts/1")
                .build();
        
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }
            
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (!response.isSuccessful()) {
                    throw new IOException("Unexpected code: " + response);
                }
                
                String responseBody = response.body().string();
                System.out.println("Async Response: " + responseBody);
                
                // 注意:在異步回調(diào)中需要手動(dòng)關(guān)閉response body
                response.close();
            }
        });
        
        System.out.println("Request sent asynchronously...");
    }
    
    // 多個(gè)異步請(qǐng)求并行執(zhí)行
    public static void multipleAsyncRequests() {
        String[] urls = {
            "https://jsonplaceholder.typicode.com/posts/1",
            "https://jsonplaceholder.typicode.com/posts/2",
            "https://jsonplaceholder.typicode.com/posts/3"
        };
        
        for (String url : urls) {
            Request request = new Request.Builder().url(url).build();
            
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    System.err.println("Request failed: " + e.getMessage());
                }
                
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (response.isSuccessful()) {
                        System.out.println("Response from " + call.request().url() + ": " + 
                                response.body().string().substring(0, 50) + "...");
                    }
                    response.close();
                }
            });
        }
    }
}

7.高級(jí)功能和配置

涵蓋:添加認(rèn)證token、重試攔截器、下載文件和使用Cookie

public class AdvancedFeatures {
    
    // 自定義配置的Client
    private static final OkHttpClient customClient = new OkHttpClient.Builder()
            .connectTimeout(15, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .writeTimeout(30, TimeUnit.SECONDS)
            .addInterceptor(chain -> {
                // 添加認(rèn)證token
                Request original = chain.request();
                Request authenticated = original.newBuilder()
                        .header("Authorization", "Bearer " + getAuthToken())
                        .build();
                return chain.proceed(authenticated);
            })
            .addInterceptor(chain -> {
                // 重試攔截器
                int maxRetries = 3;
                int retryCount = 0;
                Response response = null;
                
                while (retryCount < maxRetries) {
                    try {
                        response = chain.proceed(chain.request());
                        if (response.isSuccessful() || retryCount == maxRetries - 1) {
                            break;
                        }
                    } catch (IOException e) {
                        if (retryCount == maxRetries - 1) {
                            throw e;
                        }
                    }
                    retryCount++;
                    System.out.println("Retrying request, attempt: " + (retryCount + 1));
                }
                return response;
            })
            .build();
    
    private static String getAuthToken() {
        return "your-auth-token";
    }
    
    // 下載文件
    public static void downloadFile() throws IOException {
        Request request = new Request.Builder()
                .url("https://httpbin.org/image/jpeg")
                .build();
        
        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                byte[] fileData = response.body().bytes();
                // 保存文件到本地
                // Files.write(Paths.get("image.jpg"), fileData);
                System.out.println("File downloaded, size: " + fileData.length + " bytes");
            }
        }
    }
    
    // 使用Cookie
    public static void withCookieJar() {
        CookieJar cookieJar = new CookieJar() {
            private final List<Cookie> cookies = new ArrayList<>();
            
            @Override
            public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
                this.cookies.addAll(cookies);
            }
            
            @Override
            public List<Cookie> loadForRequest(HttpUrl url) {
                return cookies;
            }
        };
        
        OkHttpClient clientWithCookies = new OkHttpClient.Builder()
                .cookieJar(cookieJar)
                .build();
    }
}

8.OkHttp工具類(lèi)

簡(jiǎn)單Mini版的使用示例

public class OkHttpUtils {
    private static final OkHttpClient client = new OkHttpClient();
    
    public static String doGet(String url) throws IOException {
        Request request = new Request.Builder().url(url).build();
        
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Request failed: " + response);
            }
            return response.body().string();
        }
    }
    
    public static String doPost(String url, String json) throws IOException {
        RequestBody body = RequestBody.create(
                json, 
                MediaType.parse("application/json; charset=utf-8")
        );
        
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Request failed: " + response);
            }
            return response.body().string();
        }
    }
    
    // 使用示例
    public static void main(String[] args) {
        try {
            // GET請(qǐng)求
            String getResponse = doGet("https://jsonplaceholder.typicode.com/posts/1");
            System.out.println("GET Response: " + getResponse);
            
            // POST請(qǐng)求
            String json = "{\"title\":\"test\",\"body\":\"content\",\"userId\":1}";
            String postResponse = doPost("https://jsonplaceholder.typicode.com/posts", json);
            System.out.println("POST Response: " + postResponse);
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

三、適用場(chǎng)景

  1. 移動(dòng)應(yīng)用開(kāi)發(fā);
  2. 微服務(wù)間的HTTP通信;
  3. 文件上傳下載;
  4. 需要精細(xì)控制HTTP請(qǐng)求的場(chǎng)景;
  5.  Web爬蟲(chóng)和數(shù)據(jù)采集。

四、性能優(yōu)勢(shì)場(chǎng)景

高并發(fā)請(qǐng)求

// 連接池復(fù)用,適合高并發(fā)
for (int i = 0; i < 1000; i++) {
    Request request = new Request.Builder()
            .url("http://api.example.com/items/" + i)
            .build();
    client.newCall(request).enqueue(callback); // 連接復(fù)用
}

五、注意事項(xiàng)

  1. 同步請(qǐng)求:使用 execute() 方法,會(huì)阻塞當(dāng)前線(xiàn)程

  2. 異步請(qǐng)求:使用 enqueue() 方法,不會(huì)阻塞當(dāng)前線(xiàn)程

  3. 請(qǐng)求構(gòu)建:使用 Request.Builder 構(gòu)建請(qǐng)求

  4. 響應(yīng)處理:注意需要手動(dòng)關(guān)閉Response body

  5. 連接池:OkHttp自動(dòng)管理連接池,提高性能

  6. 攔截器:可以添加各種攔截器實(shí)現(xiàn)日志、認(rèn)證、重試等功能

六、與其他HTTP客戶(hù)端對(duì)比

場(chǎng)景推薦工具理由
Android應(yīng)用OkHttp3官方推薦,性能優(yōu)化
Spring Boot微服務(wù)OpenFeign聲明式,集成性好
簡(jiǎn)單Java應(yīng)用OkHttp3輕量,易用
高并發(fā)爬蟲(chóng)OkHttp3連接池,異步支持
需要精細(xì)控制OkHttp3攔截器,自定義配置

到此這篇關(guān)于Java使用okhttp3發(fā)送請(qǐng)求的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Java okhttp3發(fā)送請(qǐng)求內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用Java將DOCX文檔解析為Markdown文檔的代碼實(shí)現(xiàn)

    使用Java將DOCX文檔解析為Markdown文檔的代碼實(shí)現(xiàn)

    在現(xiàn)代文檔處理中,Markdown(MD)因其簡(jiǎn)潔的語(yǔ)法和良好的可讀性,逐漸成為開(kāi)發(fā)者、技術(shù)寫(xiě)作者和內(nèi)容創(chuàng)作者的首選格式,然而,許多文檔仍然以Microsoft Word的DOCX格式保存,本文將介紹如何使用Java和相關(guān)庫(kù)將DOCX文檔解析為Markdown文檔,需要的朋友可以參考下
    2025-04-04
  • spring gateway如何解決跨域問(wèn)題

    spring gateway如何解決跨域問(wèn)題

    這篇文章主要介紹了spring gateway如何解決跨域問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • SpringCloud?openfeign聲明式服務(wù)調(diào)用實(shí)現(xiàn)方法介紹

    SpringCloud?openfeign聲明式服務(wù)調(diào)用實(shí)現(xiàn)方法介紹

    在springcloud中,openfeign是取代了feign作為負(fù)載均衡組件的,feign最早是netflix提供的,他是一個(gè)輕量級(jí)的支持RESTful的http服務(wù)調(diào)用框架,內(nèi)置了ribbon,而ribbon可以提供負(fù)載均衡機(jī)制,因此feign可以作為一個(gè)負(fù)載均衡的遠(yuǎn)程服務(wù)調(diào)用框架使用
    2022-12-12
  • SpringBoot整合Dubbo框架,實(shí)現(xiàn)RPC服務(wù)遠(yuǎn)程調(diào)用

    SpringBoot整合Dubbo框架,實(shí)現(xiàn)RPC服務(wù)遠(yuǎn)程調(diào)用

    Dubbo是一款高性能、輕量級(jí)的開(kāi)源Java RPC框架,它提供了三大核心能力:面向接口的遠(yuǎn)程方法調(diào)用,智能容錯(cuò)和負(fù)載均衡,以及服務(wù)自動(dòng)注冊(cè)和發(fā)現(xiàn)。今天就來(lái)看下SpringBoot整合Dubbo框架的步驟
    2021-06-06
  • Spring ApplicationListener的使用詳解

    Spring ApplicationListener的使用詳解

    這篇文章主要介紹了Spring ApplicationListener的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • 一文帶你掌握SpringBoot中常見(jiàn)定時(shí)任務(wù)的實(shí)現(xiàn)

    一文帶你掌握SpringBoot中常見(jiàn)定時(shí)任務(wù)的實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了Spring?Boot中定時(shí)任務(wù)的基本用法、高級(jí)特性以及最佳實(shí)踐,幫助開(kāi)發(fā)人員更好地理解和應(yīng)用定時(shí)任務(wù),提高系統(tǒng)的穩(wěn)定性和可靠性,需要的可以參考下
    2024-03-03
  • Java內(nèi)存映射 大文件輕松處理

    Java內(nèi)存映射 大文件輕松處理

    這篇文章主要介紹了Java內(nèi)存映射 大文件輕松處理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Java中的信號(hào)量Semaphore詳細(xì)解讀

    Java中的信號(hào)量Semaphore詳細(xì)解讀

    這篇文章主要介紹了Java中的信號(hào)量Semaphore詳細(xì)解讀,Java信號(hào)量機(jī)制可以用來(lái)保證線(xiàn)程互斥,創(chuàng)建Semaphore對(duì)象傳入一個(gè)整形參數(shù),類(lèi)似于公共資源,需要的朋友可以參考下
    2023-11-11
  • Java利用SpEL表達(dá)式實(shí)現(xiàn)權(quán)限校驗(yàn)

    Java利用SpEL表達(dá)式實(shí)現(xiàn)權(quán)限校驗(yàn)

    這篇文章主要為大家詳細(xì)介紹了Java如何利用SpEL表達(dá)式實(shí)現(xiàn)權(quán)限校驗(yàn)功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01
  • 淺析java中Integer傳參方式的問(wèn)題

    淺析java中Integer傳參方式的問(wèn)題

    以下是對(duì)java中Integer傳參方式的問(wèn)題進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下
    2013-09-09

最新評(píng)論

铁力市| 旬邑县| 比如县| 吴忠市| 德令哈市| 六安市| 湟中县| 建水县| 冀州市| 金山区| 平乡县| 资源县| 福鼎市| 清苑县| 信宜市| 东辽县| 太白县| 东明县| 定州市| 和田县| 西峡县| 天祝| 济阳县| 华阴市| 祥云县| 徐汇区| 古丈县| 罗田县| 永川市| 临湘市| 尉氏县| 武川县| 旬阳县| 商河县| 合江县| 耿马| 右玉县| 六枝特区| 芦山县| 礼泉县| 临夏市|