我正在使用 HTTP 后连接,并且我的应用程序会定期连接到服务器。在某个点之后,我无法连接到服务器,并且我的所有连接都出现以下异常:
java.net.ConnectException;无法连接到 www.somedomain.com/xxx.xxx.xxx.x(端口 80):连接失败:ENETUNREACH(网络无法访问)
org.apache.http.conn.HttpHostConnectException;连接到http://www.somedomain.com被拒绝
java.net.UnknownHostException;无法解析主机“www.somedomain.com”:没有与主机名关联的地址
我怀疑服务器可能会在某个时间点后阻止我。任何想法为什么会发生这种情况?重新安装应用程序后,连接仍然被阻止。
编辑:这是发送http请求的代码
public String send() throws ClientProtocolException, IOException {
InputStream is = null;
DefaultHttpClient httpclient = null;
HttpResponse response = null;
try {
System.setProperty("http.keepAlive", "false");
httpclient = new DefaultHttpClient();
HttpProtocolParams.setUseExpectContinue(httpclient.getParams(), false);
HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() {
public boolean retryRequest(IOException exception, int executionCount,
HttpContext context) {
// retry a max of 5 times
if(executionCount >= 5){
return false;
}
if(exception instanceof NoHttpResponseException){
return true;
} else if (exception instanceof ClientProtocolException){
return true;
}
return false;
}
};
httpclient.setHttpRequestRetryHandler(retryHandler);
String proxyHost = android.net.Proxy.getDefaultHost();
int proxyPort = android.net.Proxy.getDefaultPort();
// Set Proxy params of client, if they are not the standard
if (proxyHost != null && proxyPort > 0) {
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
httpclient.getParams().setParameter(
ConnRoutePNames.DEFAULT_PROXY, proxy);
}
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(qData));
response = httpclient.execute(httppost);
is = response.getEntity().getContent();
return inputStreamToString(is);
} finally {
try {
if (is != null) {
is.close();
}
} catch (Throwable e) {
}
try {
if (response != null && response.getEntity() != null) {
response.getEntity().consumeContent();
}
} catch (Throwable e) {
}
try {
if (httpclient != null
&& httpclient.getConnectionManager() != null) {
httpclient.getConnectionManager().shutdown();
}
} catch (Throwable e) {
}
}
}