okhttp http 重定向到https
2017-02-20 14:10
162 查看
package com.adups.wql.httpredirecttohttps; import android.util.Log; import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; import com.squareup.okhttp.Response; import java.io.IOException; 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; /** * Created by bigTree on 2017/2/6. */ public class okhttp { String url = ""; static OkHttpClient okHttpClient = new OkHttpClient(); static { okHttpClient.setConnectTimeout(30, TimeUnit.SECONDS); okHttpClient.setReadTimeout(30, TimeUnit.SECONDS); okHttpClient.setWriteTimeout(30, TimeUnit.SECONDS); // okHttpClient.interceptors().add(new LoggingInterceptor()); //为false就是自定义重定向,true 就是自己内部就重定向了,只返回最后就结果 okHttpClient.setFollowRedirects(false); trustAllHosts();//设置忽略安全证书验证 okHttpClient.setHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }); } public static String run(String url) throws IOException { Request request = new Request.Builder().url(url).get().build(); Response response = okHttpClient.newCall(request).execute(); // Log.e("response.code()", "" + response.code()); // Log.e("httpUrl", "" + response.request().httpUrl()); // Log.e("response.body()", "" + response.body().string()); Log.e("Location", "" + response.headers().get("Location")); // Log.e("headers", "" + request.url()); int code = response.code(); if (code == 302) { String location = response.headers().get("Location"); run(location); } else if (response.isSuccessful()) { return response.body().string(); } else { throw new IOException("Unexpected code " + response); } return ""; } public static void trustAllHosts() { // Create a trust manager that does not validate certificate chains // Android use X509 cert TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[]{}; } public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } }}; // Install the all-trusting trust manager try { SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, trustAllCerts, new SecureRandom()); okHttpClient.setSslSocketFactory(sc.getSocketFactory()); } catch (Exception e) { e.printStackTrace(); } } }
相关文章推荐
- okhttp3 http 重定向到 https
- 使用 URL Rewrite 重定向 Http 请求至 Https
- Exchange日常管理之十一:设置OWA访问HTTP到HTTPS的重定向
- Chrome浏览器强制http重定向到https的简单方法
- 4 HTTP重定向HTTPS-方法1
- http重定向至https,80端口向443重定向
- IIS7 / IIS7.5 URL 重写 HTTP 重定向到 HTTPS
- Spring Boot工程支持HTTP和HTTPS,HTTP重定向HTTPS
- chrome 强制http重定向https
- Chrome下强制http重定向到https的方法
- HttpClient和OkHttp关于Https请求问题
- 5 HTTP重定向HTTPS-方法2
- HTTPS重定向到HTTP
- IIS7 / IIS7.5 URL 重写 HTTP 重定向到 HTTPS(转)
- iis7下实现内网OWA重定向以及http重定向到https
- 详解Golang实现http重定向https的方式
- IIS和Apache实现HTTP重定向到HTTPS
- 【http转https】其之三 IIS_URL重写_http重定向到https
- Chrome下强制http重定向到https的方法
- 如何在Spring Boot中,使用Https提供服务,并将Http请求自动重定向到Https。