HttpGet.abort()/ HttpPost.abort()并不总是立即中止

  • 地址:https://stackoverflow.com/questions/12014673/httpget-abort-httppost-abort-doesnt-always-abort-immediately
  • I’m using Apache HttpComponents 4.2.1 and I’m having trouble getting HttpGet.abort() and HttpPost.abort() to always abort immediately. It works most of the time, but occasionally the connection will block until it times out. I noticed this only happens when I explicitly set a timeout value.
  • Here’s my test code:
public static void main(String[] args) throws Exception {
    for (int i = 0; i < 10; i++) {
        testAbort();
    }
}

public static void testAbort() throws Exception {
    String urlString = "http://slow.website.com";

    final HttpGet httpGet = new HttpGet(urlString);

    Runnable runnable = new Runnable() {
        public void run() {
            try {
                HttpClient httpClient = new DefaultHttpClient();

                HttpParams httpParams = httpClient.getParams();
				//如果这里不设置timeout参数则不会发生
                HttpConnectionParams.setConnectionTimeout(httpParams, 10000); // issue doesn't occur if I comment this line
				
                httpClient.execute(httpGet);
            } 
            catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    };

    Thread t = new Thread(runnable);

    t.start();

    Thread.sleep(100);

    httpGet.abort();
}
  • Here’s a sample of the output I get:
Request already aborted
Request already aborted
Request already aborted
Socket closed
Request already aborted
Connection has been shut down
Socket closed
Socket closed
Connect to slow.website.com:80 timed out
Connect to www.website.com:80 timed out
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值