All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yy.httpdns.util.HttpUtil Maven / Gradle / Ivy

package com.yy.httpdns.util;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by huangzhilong on 2016/8/11.
 */
public class HttpUtil {
    public static final String TAG = "HttpUtil";
    public static final long CONNECT_TIMEOUT = 5000;

    private static X509TrustManager xtm = new X509TrustManager() {
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }
    };
    private static SSLContext sslContext = null;
    private static HostnameVerifier hostnameVerifier = new HostnameVerifier() {
        @Override
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    };
    private static OkHttpClient okHttpClient = new OkHttpClient();

    static {
        try {
            sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, new TrustManager[]{xtm}, new SecureRandom());
            okHttpClient = new OkHttpClient.Builder()
                    .sslSocketFactory(sslContext.getSocketFactory(), xtm)
                    .hostnameVerifier(hostnameVerifier)
                    .build();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }
    }

    public static Response syncGetIPList(String url) throws Exception {
        OkHttpClient client = okHttpClient.newBuilder()
                .connectTimeout(CONNECT_TIMEOUT, TimeUnit.MILLISECONDS)
                .build();
        Request build = new Request.Builder()
                .url(url)
                .build();
        Response response = client.newCall(build)
                .execute();
        return response;
    }

    public static Response syncSpeedTest(String url, String headerHost, long timeout) throws Exception {
        Request build;
        if (headerHost == null) {
            build = new Request.Builder()
                    .url(url)
                    .build();
        } else {
            build = new Request.Builder()
                    .url(url)
                    .header("Host", headerHost)
                    .build();
        }
        if (timeout <= 0) {
            timeout = CONNECT_TIMEOUT;
        }
        OkHttpClient client = okHttpClient.newBuilder()
                .connectTimeout(timeout, TimeUnit.MILLISECONDS)
                .readTimeout(timeout, TimeUnit.MILLISECONDS)
                .build();

        Response response = client.newCall(build)
                .execute();
        return response;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy