我想从URL获取html页面源代码。改造工作正在后期棒棒糖设备上完成。但是在棒棒糖之前的设备上,它会提供不受支持的协议错误。
代码:
private static final String TAG = "TESTTESTTESTTEST";
private static String url = "https://www.hepsiburada.com/lassa-235-65r17-108h-xl-competush-" +
"l-p-OTLST216410?magaza=LastikArt%C4%B1&utm_source=pc&utm_medium=cimri&utm_campaign" +
"=c&utm_content=c&utm_term=5083&wt_pc=cimri.c.5083.pc/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(ScalarsConverterFactory.create())
.baseUrl("https://www.google.com/")
.build();
IGetHtml iGetHtml = retrofit.create(IGetHtml.class);
Call<String> stringCall = iGetHtml.getHtml(url);
stringCall.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()) {
String responseString = response.body();
Log.e(TAG, "onResponse: " + response.body());
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Log.e(TAG, "onFailure: " + t);
}
});
}
错误
E / NativeCrypto:握手期间发生未知错误07-09 12:23:57.606
E / TESTTESTTESTTEST:onFailure:javax.net.ssl.SSLHandshakeException:javax.ne.ssl.SSLProtocolException: SSL握手已中止:ssl = 0x7cf6f320:通常在SSL库中失败 协议错误错误:14077102:SSL 例程:SSL23_GET_SERVER_HELLO:不支持的协议 (外部/openssl/ssl/s23_clnt.c:714 0x7a084894:0x00000000)
答案 0 :(得分:1)
不是因为翻新,而是因为未为较低版本的Android启用TLS和SSL支持。
https://developer.android.com/reference/javax/net/ssl/SSLSocket
您可以通过在应用中检查Google Play服务更新来在Android 4.1-4.4版本上启用TLS。您可以在Android中单独搜索ProviderInstaller。
如果它只是一个html页面,建议使用
在外部浏览器中打开它。new Intent(Intent.ACTION_VIEW, Uri.parse("your url"));
答案 1 :(得分:0)
我通过此解决方案解决了我的错误。
除了变化之外,其他一切与我在问题中所写的相同。
将此类添加到您的项目中。
public class TLSSocketFactory extends SSLSocketFactory {
private SSLSocketFactory delegate;
public TLSSocketFactory() throws KeyManagementException, NoSuchAlgorithmException {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, null, null);
delegate = context.getSocketFactory();
}
@Override
public String[] getDefaultCipherSuites() {
return delegate.getDefaultCipherSuites();
}
@Override
public String[] getSupportedCipherSuites() {
return delegate.getSupportedCipherSuites();
}
@Override
public Socket createSocket() throws IOException {
return enableTLSOnSocket(delegate.createSocket());
}
@Override
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
return enableTLSOnSocket(delegate.createSocket(s, host, port, autoClose));
}
@Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
return enableTLSOnSocket(delegate.createSocket(host, port));
}
@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
return enableTLSOnSocket(delegate.createSocket(host, port, localHost, localPort));
}
@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
return enableTLSOnSocket(delegate.createSocket(host, port));
}
@Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
return enableTLSOnSocket(delegate.createSocket(address, port, localAddress, localPort));
}
private Socket enableTLSOnSocket(Socket socket) {
if (socket != null && (socket instanceof SSLSocket)) {
((SSLSocket) socket).setEnabledProtocols(new String[]{"TLSv1.1", "TLSv1.2"});
}
return socket;
}
}
然后onCreate方法更改为此。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OkHttpClient client = new OkHttpClient();
try {
client = new OkHttpClient.Builder()
.sslSocketFactory(new TLSSocketFactory())
.build();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://www.google.com/")
.client(client)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
IGetHtml iGetHtml = retrofit.create(IGetHtml.class);
Call<String> stringCall = iGetHtml.getHtml(url);
stringCall.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()) {
Log.e(TAG, "onResponse: " + response.headers());
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Log.e(TAG, "onFailure: " + t);
}
});
}