我使用在http请求上使用代理的库来增加安全性。问题是它在Android> = KitKat上完美运行,但在JellyBean中它不通过代理。
您是否知道在该版本中使用AndroidClientHandler
是否支持代理?我知道使用AndroidClientHandler pre Lollipop不支持TLS 1.2+,但是我需要那个处理程序(事实上我继承了AndroidClientHandler以绕过SSL验证,因为代理处理了这个并且库需要它)来配置一些东西。代理如何运作。
MyCustomMessageHandler实现:
public class MyCustomMessageHandler : AndroidClientHandler
{
public override bool SupportsProxy => true;
protected override SSLSocketFactory ConfigureCustomSSLSocketFactory(HttpsURLConnection connection)
{
return this.GetBypassVerificationSSLSocketFactory();
}
protected override IHostnameVerifier GetSSLHostnameVerifier(HttpsURLConnection connection)
{
return new CustomHostnameVerifier();
}
protected override async Task SetupRequest(HttpRequestMessage request, HttpURLConnection conn)
{
this.HandleCustomPreAuthentication(conn);
await base.SetupRequest(request, conn);
}
private void HandleCustomPreAuthentication(HttpURLConnection conn)
{
var proxyAuth = "MyUsername" + ":" + "MyPassword";
var encodedProxyAuth = Base64.EncodeToString(Encoding.UTF8.GetBytes(proxyAuth), Base64Flags.Default);
conn.SetRequestProperty("Proxy-Authorization", encodedProxyAuth);
}
private SSLSocketFactory GetBypassVerificationSSLSocketFactory()
{
SSLContext sslContext;
try
{
sslContext = SSLContext.GetInstance("SSL");
sslContext.Init(new IKeyManager[0], new ITrustManager[] { new X509TrustManager() }, new SecureRandom());
return sslContext.SocketFactory;
}
catch (GeneralSecurityException e)
{
throw new RuntimeException(e);
}
}
}
public class CustomHostnameVerifier : Java.Lang.Object, IHostnameVerifier
{
public bool Verify(string hostname, ISSLSession session) => true;
}
public class X509TrustManager : Java.Lang.Object, IX509TrustManager
{
public void CheckClientTrusted(X509Certificate[] chain, string authType)
{
}
public void CheckServerTrusted(X509Certificate[] chain, string authType)
{
}
public X509Certificate[] GetAcceptedIssuers()
{
return new X509Certificate[0];
}
}
我已经尝试使用托管客户端配置代理,但它无法按预期工作;这就是我选择AndroidClientHandler
我推断它不是通过代理,因为捕获数据包我意识到< KitKat它通过Http发送请求,而> = KitKat它们都是TCP或TLSv1.2,代理是https网址。