• 使用OkHttp和Java来下载


    以下是一个使用OkHttp和Java来下载内容的下载器程序,同时使用了jshk.com.cn/get_proxy来获取代理服务器。请注意,为了简化代码,我们将忽略一些异常处理和安全性检查。

    
    import java.io.File;
    
    import java.io.FileOutputStream;
    
    import java.io.IOException;
    
    import java.net.Proxy;
    
    import java.net.URL;
    
    import java.nio.charset.StandardCharsets;
    
    import java.nio.file.Files;
    
    import java.util.concurrent.TimeUnit;
    
    import okhttp3.OkHttpClient;
    
    import okhttp3.Request;
    
    import okhttp3.Response;
    
    
    
    public class Downloader {
    
        public static void main(String[] args) {
    
            OkHttpClient client = new OkHttpClient.Builder()
    
                    .connectTimeout(30, TimeUnit.SECONDS)
    
                    .readTimeout(30, TimeUnit.SECONDS)
    
                    .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8080)))
    
                    .build();
    
    
    
            Request request = new Request.Builder()
    
                    .url("http://www.people.com.cn")
    
                    .get()
    
                    .build();
    
    
    
            try {
    
                Response response = client.newCall(request).execute();
    
                if (response.isSuccessful()) {
    
                    String content = response.body().string();
    
                    Files.write(new File("output.html").toPath(), content.getBytes(StandardCharsets.UTF_8));
    
                    System.out.println("下载完成!");
    
                } else {
    
                    System.out.println("下载失败:" + response.message());
    
                }
    
            } catch (IOException e) {
    
                System.out.println("下载失败:" + e.getMessage());
    
            }
    
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79

    这个程序首先创建一个OkHttpClient实例,并设置了连接超时和读取超时时间。然后接下来,创建一个Request实例。然后使用OkHttpClient实例的newCall方法发送请求,并使用execute方法执行请求。如果请求成功,则将返回的内容保存到一个名为output.html的文件中。

  • 相关阅读:
    包管理机制、变量声明方式的区别
    FreeRTOS 中如何定位 HardFault?
    小土堆pytorch学习笔记
    SpringBoot中幕——配置文件properties与yml
    React 渲染流程分析
    用R语言制作交互式图表和地图
    nodejs定时任务
    React基础知识点
    解决java.io.FileNotFoundException: HADOOP_HOME and hadoop.home.dir are unset.的错误
    GitHub最新发布,阿里十年架构师手写版spring全家桶笔记全新开源
  • 原文地址:https://blog.csdn.net/D0126_/article/details/133919866