握手使用okHttp失败,但使用HttpURLConnection工作

我试图发送请求到服务器(发送请求测试,实际上我需要发送一个请求到同一台服务器。如果得到工作,发布将工作)

链接到服务器是https://bits-bosm.org/2017/registrations/signup/

问题是当我发送请求使用okHttp,我得到一个失败的回应说握手失败。

这里是我使用的代码发送请求使用okHttp(在kotlin)

val request = Request.Builder() .url("https://bits-bosm.org/2017/registrations/signup/") .build() okHttpClient.newCall(request).enqueue(object : Callback { override fun onFailure(call: Call?, e: IOException?) { val mMessage = e?.message?.toString() Log.w("failure Response", mMessage) } override fun onResponse(call: Call?, response: Response?) { val mMessage = response?.body()?.string() Log.e("Message", mMessage) } }) 

但是,如果我使用HttpUrlConnection发送获取请求到同一台服务器,我得到的答复。

这里是相同的代码(Java)

 private static final String USER_AGENT = "Mozilla/5.0"; private static final String GET_URL = "https://bits-bosm.org/2017/registrations/signup/"; static void sendGET() throws IOException { URL obj = new URL(GET_URL); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); con.setRequestProperty("User-Agent", USER_AGENT); int responseCode = con.getResponseCode(); System.out.println("GET Response Code :: " + responseCode); if (responseCode == HttpURLConnection.HTTP_OK) { // success BufferedReader in = new BufferedReader(new InputStreamReader( con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // print result Log.e("Result", response.toString()); } else { System.out.println("GET request not worked"); } } 

从我搜索的互联网和我可以推断,问题是网站是使用自我证书签名,并okHttp不允许他们。 我甚至尝试使用我在互联网上发现的代码片段,不检查证书(自定义SSLSocketFactory)和一些更多的解决方案,但没有一个工作。 另外我现在不在乎安全,我只是想让它工作。 但是我无法访问后端,也无法更改/删除SSL安全性。

有什么可以做到这一点的工作? 有什么我失踪?

以下是作为解决方法广泛使用的不安全的OkHttpClient。

不要在生产中使用它,只是为了开发目的。

 import java.security.SecureRandom; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import okhttp3.OkHttpClient; public class Http { private final static String SSL = "SSL"; private static OkHttpClient InsecureHttpClient; public static OkHttpClient client () { if (InsecureHttpClient == null) { try { InsecureHttpClient = insecureOkHttpClient (); } catch (Exception e) { e.printStackTrace (); } } return InsecureHttpClient; } private static OkHttpClient insecureOkHttpClient () throws Exception { TrustManager [] trustAllCerts = new TrustManager [] { trustManager () }; SSLContext sslContext = SSLContext.getInstance (SSL); sslContext.init (null, trustAllCerts, new SecureRandom ()); SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory (); OkHttpClient.Builder builder = new OkHttpClient.Builder (); builder.sslSocketFactory (sslSocketFactory, (X509TrustManager)trustAllCerts [0]); builder.hostnameVerifier (hostnameVerifier ()); return builder.build (); } private static TrustManager trustManager () { return new X509TrustManager () { @Override public void checkClientTrusted (X509Certificate [] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted (X509Certificate[] chain, String authType) throws CertificateException { } @Override public X509Certificate [] getAcceptedIssuers () { return new X509Certificate [] { }; } }; } private static HostnameVerifier hostnameVerifier () { return new HostnameVerifier () { @Override public boolean verify (String hostname, SSLSession session) { return true; } }; } } 

然后,你明显地使用上面的客户端,如下面的测试代码:(顺便说一句,你的网址)

 final Request request = new Request.Builder () .url ("https://bits-bosm.org/2017/registrations/signup/") .get () .addHeader ("Accept", "text/html") .build (); final OkHttpClient httpClient = Http.client (); new Thread (new Runnable () { @Override public void run () { try { Response response = httpClient.newCall (request).execute (); Logger.error (MainActivity.class.getSimpleName () + " --> Http Response", response.body ().string ()); } catch (IOException e) { e.printStackTrace (); } } }).start ();