package com.bytedance.adsdk.ro.yz;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;

/* JADX INFO: loaded from: classes.dex */
public class fm implements yz {
    private final HttpURLConnection fm;

    public fm(HttpURLConnection httpURLConnection) {
        this.fm = httpURLConnection;
    }

    @Override // java.io.Closeable, java.lang.AutoCloseable
    public void close() {
        this.fm.disconnect();
    }

    @Override // com.bytedance.adsdk.ro.yz.yz
    public boolean fm() {
        try {
            return this.fm.getResponseCode() / 100 == 2;
        } catch (IOException unused) {
            return false;
        }
    }

    @Override // com.bytedance.adsdk.ro.yz.yz
    public String lb() {
        return this.fm.getContentType();
    }

    @Override // com.bytedance.adsdk.ro.yz.yz
    public InputStream ro() throws IOException {
        return this.fm.getInputStream();
    }

    @Override // com.bytedance.adsdk.ro.yz.yz
    public String yz() {
        try {
            if (fm()) {
                return null;
            }
            return "Unable to fetch " + this.fm.getURL() + ". Failed with " + this.fm.getResponseCode() + "\n" + fm(this.fm);
        } catch (IOException e10) {
            return e10.getMessage();
        }
    }

    private String fm(HttpURLConnection httpURLConnection) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getErrorStream()));
        StringBuilder sb = new StringBuilder();
        while (true) {
            try {
                String line = bufferedReader.readLine();
                if (line != null) {
                    sb.append(line);
                    sb.append('\n');
                } else {
                    try {
                        break;
                    } catch (Exception unused) {
                    }
                }
            } finally {
                try {
                    bufferedReader.close();
                } catch (Exception unused2) {
                }
            }
        }
        return sb.toString();
    }
}
