- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我遇到了 Android 上 DefaultHttpClient 的超时问题。我正在尝试使用以下代码设置超时:
HttpClient client = new DefaultHttpClient();
HttpParams httpParameters = client.getParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 4000);
HttpConnectionParams.setSoTimeout(httpParameters, 4000);
但是如果设备连接到没有互联网连接的网络,则永远不会触发超时,并且 http 请求的执行永远不会抛出任何超时异常。我正在执行 http 请求如下:
HttpResponse httpResponse = client.execute(request);
我还尝试使用以下几行设置 HttpRequest 的超时:
HttpRequestBase request = ...
request.setParams(httpParameters);
Android 似乎忽略了超时设置,当在没有互联网连接的网络上执行 http 请求时,所有请求都会在大约 20 秒后失败,而不是在我的超时设置之后。
我还尝试在并行线程超时后关闭所有互联网连接并中止 http 请求。我使用了以下代码:
HttpClient client = new DefaultHttpClient();
HttpParams httpParameters = client.getParams();
HttpRequestBase request = ...
request.setParams(httpParameters);
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutReal);
HttpConnectionParams.setSoTimeout(httpParameters, timeoutReal);
request.setParams(httpParameters);
((DefaultHttpClient) client).setParams(httpParameters);
Thread t = new Thread(){
public void run()
{
try
{
Thread.sleep(4000);
request.abort();
client.getConnectionManager().closeExpiredConnections();
client.getConnectionManager().closeIdleConnections(4000,TimeUnit.MILLISECONDS);
client.getConnectionManager().shutdown();
Log.i("TEST SHUTDOWN","SHUT DOWN ALL CONNECTIONS");
}
catch (InterruptedException e)
{
}
}
};
try
{
t.start();
HttpResponse httpResponse = client.execute(request);
}
catch (Exception e)
{
Log.i("TEST SHUTDOWN","EXCEPTION "+e);
}
finally
{
t.interrupt();
}
但是即使我从日志中看到请求被中止并且连接管理器被关闭,请求的执行也没有被中断/中止并且在超时设置时没有引发异常。请求总是在 20 秒后结束。
知道为什么吗?
最佳答案
您可能看到的是 DNS 查找超时。当您的 HTTP 客户端尝试建立连接时,它所做的第一件事就是尝试将您的 URL 的主机名解析为 IP 地址。这样做时,它不会考虑您的超时设置(这些超时值仅在尝试实际建立套接字连接时使用)。如果您没有良好的 Internet 连接,您的 DNS 查找将停止直到超时。一旦发生这种情况,您的 HTTP 请求应该立即失败并返回 UnknownHostException
。
不幸的是,您无法控制 DNS 超时,因此解决问题的唯一方法是首先确定您的 DNS 解析是否有效。您应该在单独的线程中执行此操作,如果您没有在几秒钟内成功解析主机,您就知道您的 Internet 连接不可靠,您甚至不必尝试 HTTP 请求。
所以你可以尝试这样的事情:
HttpClient client = new DefaultHttpClient();
HttpParams httpParameters = client.getParams();
HttpRequestBase request = ...
request.setParams(httpParameters);
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutReal);
HttpConnectionParams.setSoTimeout(httpParameters, timeoutReal);
request.setParams(httpParameters);
((DefaultHttpClient) client).setParams(httpParameters);
// The thread that is waiting to execute the HTTP request
final Thread waitingThread = Thread.currentThread();
Thread t = new Thread() {
boolean running = true;
public void run() {
try {
// Try to resolve the host name
InetAddress addr = InetAddress.getByName (hostname);
// Successful resolution, notify the waiting thread
if (running) {
// Signal the waiting thread that it can do the HTTP request now
waitingThread.interrupt();
}
} catch (Exception e) {
// Some problem, just ignore it
}
}
};
try {
// Start name resolution
t.start();
// Sleep for as long as we are willing to wait for the DNS resolution
Thread.sleep(4000);
// If we slept the entire time without getting interrupted, the DNS resolution took too long
// so assume we have no connectivity.
t.running = false; // We don't want to be interrupted anymore
// Don't even bother trying the HTTP request now, we've used up all the time we have
} catch (InterruptedException ie) {
// We got interrupted, so the DNS resolution must have been successful. Do the HTTP request now
HttpResponse httpResponse = client.execute(request);
}
我是在没有尝试的情况下编写这段代码的,所以请原谅任何拼写错误或遗漏的分号。你应该明白了。
关于Android 忽略 DefaultHttpClient 超时参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27185977/
我尝试在 Android Java 环境中检索对 URL 的 POST 响应: 这是我的代码: try{ DefaultHttpClient
我正在使用 DefaultHttpClient 的 singleton 实例与 PoolingClientConnectionManager 一起执行大量(每秒多次)HTTP 操作以多线程的方式。我的
我有一段使用 DefaultHttpClient 的代码,它发出登录网站的发布请求。 但是随后的请求似乎忘记了我已登录的事实。 如何打开我正在使用的 DefaultHttpClient 的自动 coo
我想将 DefaultHttpClient 转换为单例类 public class HttpClient { private static final String TAG = "&&----HT
我有一个 Apache 提供的 DefaultHttpClient,我创建并执行不同的请求。 问题是与服务器的连接可能会建立相当长的时间,并且我的程序的用户可能想要取消此过程。 是否有任何有效的方法可
我对 DefaultHttpClient 中的超时如何工作感到有点困惑。 我正在使用这段代码: private DefaultHttpClient createHttpClient() {
我在我当前的应用程序中使用 DefaultHttpClient。 我读过这篇文章,其中指出 DefaultHttpClient 已弃用:http://developer.android.com/ref
我想做什么 大家好,我正在尝试创建一个应用程序,我可以在其中查看客户给我的订单。为此,我在我的服务器上创建了一个接口(interface),我可以在该接口(interface)上发送 post/get
我的问题是,如果我没有指定,使用 DefaultHttpClient 发出的请求的默认超时是多少。 如果没有这样的代码 HttpParams my_httpParams = new BasicHttp
我正在尝试使用 Android 的 DefaultHTTPClient 获取页面并使用 Jsoup 解析它。我收到一个非常奇怪的响应,其中 中的所有 HTML和标签被编码成某种东西。
我想将带有两个参数的 POST 请求从 JsonObject 发送到 python API,该 API 响应 True 或 False,但它根本不起作用... 我的代码: // Create a ne
我正在使用 DefaultHttpClient 将数据发送到网络...使用以下代码块发送数据: nameValuePairs.add(new BasicNameValuePair(name, valu
这是我的 AsyncTask 类: public class JSONParser extends AsyncTask { public interface MyCallbackInterfac
我是 servlet 应用程序的新手,正在尝试学习该主题。在途中,我编写了一个名为 FormWebServlet 的 servlet 类,它使用 org.apache.http.impl.client
尝试了几乎所有方法,但没有成功使 DefaultHttpClient() 使用过期的 SSL 证书。有没有人有一些例子或可以提供帮助的东西。 Android SDK 8 最佳答案 您可以使用自定义 T
我遇到了 Android 上 DefaultHttpClient 的超时问题。我正在尝试使用以下代码设置超时: HttpClient client = new DefaultHttpClient();
在我遇到很多抢先式身份验证问题之后,我终于让它工作了。现在下一个问题。我想用它获取一个文件,但我不知道如何获取。我认为文件数据可能在变量响应中,但事实并非如此。任何想法这可能如何工作?我尝试了好几天都
我如何将字符串数据 (JSONObject.toString()) 发送到 url。我想在 util 类中编写一个静态方法来执行此操作。我希望方法签名如下 public static String p
我正在做一个项目,我需要调用我的服务,我的服务将以 JSON 格式返回数据。而且我不需要将这个 JSON 响应序列化到任何 POJO,我只需要将数据作为 String 取回。这个应用程序对性能非常关键
在我的代码中有这个 Log.d("WFlog (executeRequest)", request.toString()) ; httpResponse = clien
我是一名优秀的程序员,十分优秀!