Android 如何将不安全的 HttpClient 传递给 Volley 请求
How to pass unsafe HttpClient to Volley request in Android
我正在开发 Android 应用程序,我的应用程序包含 Volley 库以进行 API 调用,但我面临 SSL:self 签名证书问题的挑战 url 我在代码中使用的。但它通过传递 "unsafe HttpClient" 在另一个带有 Retrofit 库的项目中工作,所以请告诉我如何解决此问题或如何将 "unsafe HttpClient" 传递给 Volley 以进行 API 调用。
尝试将此添加到 AndoidManifest.xml 应用程序标签中:-
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
我发现它对我有用,请让我知道这里有什么问题。这里是调用需要添加的代码。
final String url = "YOUR URL";
RequestQueue queue = Volley.newRequestQueue(this, getHttpStack(url));
// prepare the Request
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// display response
Log.e("test", "Response" + response.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("test", "Error.Response: " + error.getMessage());
}
}
);
queue.add(getRequest);
这里是方法,需要在代码中添加。
private HurlStack getHttpStack(String urlToValidate) {
URL url = null;
try {
url = new URL(urlToValidate);
} catch (MalformedURLException e) {
e.printStackTrace();
}
final String baseUrl = url.getAuthority();
return new HurlStack() {
@Override
protected HttpURLConnection createConnection(final java.net.URL url)
throws IOException {
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) super
.createConnection(url);
try {
httpsURLConnection
.setSSLSocketFactory(handleSSLHandshake(httpsURLConnection));
httpsURLConnection.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
//return true;
HostnameVerifier hv =
HttpsURLConnection.getDefaultHostnameVerifier();
return hv.verify(baseUrl, session);
}
});
} catch (Exception e) {
e.printStackTrace();
}
return httpsURLConnection;
}
};
}
@SuppressLint("TrulyRandom")
public static SSLSocketFactory handleSSLHandshake(HttpsURLConnection connection) {
try {
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}};
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
Log.e("test", "RETURN TRUE: ");
return true;
}
});
sc.init(null, trustAllCerts, new java.security.SecureRandom());
SSLSocketFactory newFactory = sc.getSocketFactory();
connection.setSSLSocketFactory(newFactory);
} catch (Exception ignored) {
}
return connection.getSSLSocketFactory();
}
我正在开发 Android 应用程序,我的应用程序包含 Volley 库以进行 API 调用,但我面临 SSL:self 签名证书问题的挑战 url 我在代码中使用的。但它通过传递 "unsafe HttpClient" 在另一个带有 Retrofit 库的项目中工作,所以请告诉我如何解决此问题或如何将 "unsafe HttpClient" 传递给 Volley 以进行 API 调用。
尝试将此添加到 AndoidManifest.xml 应用程序标签中:-
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
我发现它对我有用,请让我知道这里有什么问题。这里是调用需要添加的代码。
final String url = "YOUR URL";
RequestQueue queue = Volley.newRequestQueue(this, getHttpStack(url));
// prepare the Request
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// display response
Log.e("test", "Response" + response.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("test", "Error.Response: " + error.getMessage());
}
}
);
queue.add(getRequest);
这里是方法,需要在代码中添加。
private HurlStack getHttpStack(String urlToValidate) {
URL url = null;
try {
url = new URL(urlToValidate);
} catch (MalformedURLException e) {
e.printStackTrace();
}
final String baseUrl = url.getAuthority();
return new HurlStack() {
@Override
protected HttpURLConnection createConnection(final java.net.URL url)
throws IOException {
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) super
.createConnection(url);
try {
httpsURLConnection
.setSSLSocketFactory(handleSSLHandshake(httpsURLConnection));
httpsURLConnection.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
//return true;
HostnameVerifier hv =
HttpsURLConnection.getDefaultHostnameVerifier();
return hv.verify(baseUrl, session);
}
});
} catch (Exception e) {
e.printStackTrace();
}
return httpsURLConnection;
}
};
}
@SuppressLint("TrulyRandom")
public static SSLSocketFactory handleSSLHandshake(HttpsURLConnection connection) {
try {
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}};
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
Log.e("test", "RETURN TRUE: ");
return true;
}
});
sc.init(null, trustAllCerts, new java.security.SecureRandom());
SSLSocketFactory newFactory = sc.getSocketFactory();
connection.setSSLSocketFactory(newFactory);
} catch (Exception ignored) {
}
return connection.getSSLSocketFactory();
}