gpt4 book ai didi

android - 具有身份验证的服务器上的 ECONNRESET(对等方重置连接)

转载 作者:行者123 更新时间:2023-11-29 01:41:47 26 4
gpt4 key购买 nike

这是我的下载代码:

            File file = new File(dir, fileName);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(downloadListHttpGet
.get(index));
InputStream input = response.getEntity().getContent();
OutputStream output = new FileOutputStream(
memoryDirectory + fileName);

byte data[] = new byte[1024];
int count;

while ((count = input.read(data)) != -1) {

output.write(data, 0, count);

}

output.flush();
output.close();
input.close();

这段代码工作正常,但是对于经过身份验证的服务器,在下载开始将近 40 秒后,我得到“ECONNRESET(连接由对等方重置)”异常。我认为它发生在“input.read(data)”部分。我找了很多!!!有些人说这是服务器故障。这可能是真的,但我必须在客户端解决这个问题。我试图在“catch” block 中运行新线程但是有一个问题:当异常发生时,它不会立即转到“catch” block ,它需要将近 2 分钟。我试过这个解决方案: Getting "SocketException : Connection reset by peer" in Android但它不起作用。

最佳答案

尝试使用此类 getHttpClient() 而不是 new DefaultHttpClient():

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;

public class MySSLSocketFactory extends SSLSocketFactory {
SSLContext sslContext = SSLContext.getInstance("TLS");
public static int ConnectionTimeoutInSeconds = 45;
public static boolean DisableSSLcertificateCheck = true;

public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
super(truststore);

TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}

public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}

public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}

};

sslContext.init(null, new TrustManager[] { tm }, null);
}

@Override
public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
}

@Override
public Socket createSocket() throws IOException {
return sslContext.getSocketFactory().createSocket();
}

public static HttpClient getHttpClient() {
try {

HttpParams params = new BasicHttpParams();

// Turn off stale checking. Our connections break all the time anyway,
// and it's not worth it to pay the penalty of checking every time.
HttpConnectionParams.setStaleCheckingEnabled(params, false);

// Default connection and socket timeout of 20 seconds. Tweak to taste.
HttpConnectionParams.setConnectionTimeout(params, ConnectionTimeoutInSeconds * 1000);
HttpConnectionParams.setSoTimeout(params, ConnectionTimeoutInSeconds * 1000);
HttpConnectionParams.setSocketBufferSize(params, 8192);

// Don't handle redirects -- return them to the caller. Our code
// often wants to re-POST after a redirect, which we must do ourselves.
HttpClientParams.setRedirecting(params, false);

SSLSocketFactory mySSLSocketFactory = SSLSocketFactory.getSocketFactory();

// disable ssl check on debug
if (DisableSSLcertificateCheck ) {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
mySSLSocketFactory = new MySSLSocketFactory(trustStore);
HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
mySSLSocketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
}

SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schemeRegistry.register(new Scheme("https", mySSLSocketFactory, 443));
ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry);

return new DefaultHttpClient(manager, params);
} catch (Exception e) {
return new DefaultHttpClient();
}
}

}

它处理 SSL,使您能够忽略 SSL 证书并允许您手动更改超时。我将从“禁用 ssl 检查”开始并将超时设置为 90(秒)。

您可能尝试进行身份验证的另一种方式与 Bing 搜索 API 身份验证相同(您需要下载并包含 commons-codec-1.9.jar):

public class SearchAsyncTask extends AsyncTask<Void, Void, Void> {

private final String TAG = getClass().getName();

@Override
protected Void doInBackground(Void... params) {
try {
String bingUrl = "https://api.datamarket.azure.com/Bing/SearchWeb/v1/Web?Query=pinhassi";

String accountKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
byte[] accountKeyBytes = Base64.encodeBase64((accountKey + ":" + accountKey).getBytes());
String accountKeyEnc = new String(accountKeyBytes);

URL url = null;
url = new URL(bingUrl);

URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Authorization", "Basic " + accountKeyEnc);
InputStream response = urlConnection.getInputStream();
String res = readStream(response);
Log.d(TAG, res);
//conn.disconnect();

} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, e.getMessage());
}

return null;
}

private String readStream(InputStream in) {
BufferedReader reader = null;
StringBuilder sb = new StringBuilder();
try {
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
//System.out.println(line);
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}

}

关于android - 具有身份验证的服务器上的 ECONNRESET(对等方重置连接),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24104746/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com