我正在使用HTTP post连接,我的应用程序会定期连接到服务器。在某一点之后,我无法连接到服务器,我得到了所有连接的这些例外:
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) {
}
}
}
答案 0 :(得分:1)
您可以使用dig或nslookup验证DNS服务器。您可以在操作系统中配置已知良好的DNS服务器,如8.8.8.8。 异常错误消息也可能具有误导性:尝试使用wireshark捕获网络流量,然后您将看到DNS查询是否完全没有返回,“未找到”,或DNS是否正常,但服务器主机拒绝您的请求。
您还可以通过将www.somedomain.com添加到您的主机文件及其IP地址来绕过DNS不确定性。