如何为 HTTP Post 添加超时

发布于 2024-12-31 23:47:40 字数 1279 浏览 5 评论 0原文

为什么代码对我不起作用,即使连接已经过时,(服务器很长时间没有响应,代码也没有抛出任何异常!

try{

    HttpParams httpParameters = new BasicHttpParams();  
    HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
    HttpConnectionParams.setSoTimeout(httpParameters, 3000);
    HttpClient client = new DefaultHttpClient(httpParameters);              
    HttpPost post = new HttpPost("url");  
    post.setParams(httpParameters);
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("ab ", cd));
    post.setEntity(new UrlEncodedFormEntity(nameValuePairs));           
    HttpResponse response = client.execute(post);           
    BufferedReader rd = new BufferedReader(new 

    InputStreamReader(response.getEntity().getContent()));              
    while ((line = rd.readLine()) != null) 
   {

   }
  }
   catch(java.net.SocketTimeoutException e4)
   {
      Toast.makeText(xyz.this, "Your connection is stale", 10000).show();
   }
   catch (IOException e2) 
   {
  Log.d("G22", "Entered Exception");    
   }
   catch (Exception e2) 
   {
      Toast.makeText(xyz.this, "Your connection is stale..", 10000).show();
      e2.printStackTrace();
      Log.d("G22", "Entered Exception");    
   }

Why the code is Not working for me, Even the connection is stale, (there is no response from the server for a long time, the code is not throwing any exception!

try{

    HttpParams httpParameters = new BasicHttpParams();  
    HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
    HttpConnectionParams.setSoTimeout(httpParameters, 3000);
    HttpClient client = new DefaultHttpClient(httpParameters);              
    HttpPost post = new HttpPost("url");  
    post.setParams(httpParameters);
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("ab ", cd));
    post.setEntity(new UrlEncodedFormEntity(nameValuePairs));           
    HttpResponse response = client.execute(post);           
    BufferedReader rd = new BufferedReader(new 

    InputStreamReader(response.getEntity().getContent()));              
    while ((line = rd.readLine()) != null) 
   {

   }
  }
   catch(java.net.SocketTimeoutException e4)
   {
      Toast.makeText(xyz.this, "Your connection is stale", 10000).show();
   }
   catch (IOException e2) 
   {
  Log.d("G22", "Entered Exception");    
   }
   catch (Exception e2) 
   {
      Toast.makeText(xyz.this, "Your connection is stale..", 10000).show();
      e2.printStackTrace();
      Log.d("G22", "Entered Exception");    
   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〗斷ホ乔殘χμё〖 2025-01-07 23:47:40

您必须使用 HttpClientHttpPost 吗?您可以使用 HttpURLConnection,它具有 setConnectTimeout(int) 方法:

参见我的回答此处查看使用 HttpsURLConnection 进行 POST 操作的示例。

Do you have to use HttpClient and HttpPost? You could use an HttpURLConnection, which has the setConnectTimeout(int) method:

See my answer here for an example of using HttpsURLConnection for a POST operation.

鹤舞 2025-01-07 23:47:40

你可以使用这个代码

    HttpTransportSE MyandroidHttpTransport = new HttpTransportSE("http://www.webservicex.net/ConvertWeight.asmx",50000);
    //50000 is the timeout for this connection

U can Use this code

    HttpTransportSE MyandroidHttpTransport = new HttpTransportSE("http://www.webservicex.net/ConvertWeight.asmx",50000);
    //50000 is the timeout for this connection
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文