2017-07-14 32 views
3

有关如何杀死发出http post请求并等待获得响应的线程的任何建议。我想从另一个并行运行的线程中杀死这个线程,然后它可以收到来自请求的任何响应。我有建议关闭或中止http发布请求,但没有锻炼,任何人都可以建议如何中止http请求在这种情况下或任何其他方式来实现这一点。如何取消从另一个线程发出http请求的线程

+1

尝试'Thread.interrupt'。 – talex

+0

停止线程的唯一方法是中断线程。线程的run方法会正确响应中断,否则不会。如果没有,那你就没有别的办法了。 – VGR

+0

https://stackoverflow.com/questions/3000214/java-http-client-request-with-defined-timeout。你有没有看过异步HTTP? – efekctive

回答

1

发出请求的线程在等待服务器响应时被阻塞。如果你进行线程转储并查看,它将在一些read()方法中,但不在Thread.sleep中。所以调用interrupt()不会有效果(除非它是一个InterruptibleChannel)。 您是否可以访问发出POST请求的套接字或输出流?如果是这样,你可以关闭它,这将带来一个例外的读取等待。 你有可以放置的代码示例吗?

+0

你是对的,如果我们在等待响应期间在线程上执行thread.getState(),我们将获得状态为RUNNING。由于这种情况,当中断被触发时,线程的中断标志被设置,但是线程在完成请求后将被优雅地终止。 –

+0

IAM使用Apache HTTP客户端,我不能使用asynchttp。我尝试请求 –

+0

IAM使用Apache HTTP客户端,我不能使用asynchttp。我尝试使用一个线程发送请求和前:ReqThread和取消侦听器线程前:取消线程和我有一个共享变量对象持有httpPost对象。实际的请求发生在类A中,它使用ReqThread发送的httpPost发送请求。我在CancelThread中取消事件监听实现,读取由共享对象中的ReqThreaad设置的httpPost,以发出** httppost.abort()**。这是Iam将httpPost对象设为null的问题。 –

1

Helloo大家好,终于得到了这个闭合的socket思想实现和工作。下面是我试过的片段。

我的请求的线程

package com.pkg.myhttptest; 

import org.apache.http.client.methods.HttpPost; 

public class RequestThread implements Runnable{ 

    Message msg = null; 
    Util util = null; 
    public RequestThread(Message msg, Util util) { 
     this.msg = msg; 
     this.util = util; 
    } 
    public void run() { 
     System.out.println("its request thread"); 
     util.print(msg); 
    } 



} 

取消线程

public class CancelThread implements Runnable { 
    Message msg = null; 
    Util util = null; 
    public CancelThread(Message msg, Util util) { 
     this.msg = msg; 
     this.util = util; 
    } 
    public void run() { 
     System.out.println("its cancel thread"); 
     int i=0; 
     while(i<5){ 
      System.out.println("looping" + i); 
      i++; 
     } 
     System.out.println(msg.getHttpost() + "In cancelled"); 
     Header[] hdr = msg.getHttpost().getAllHeaders(); 
     System.out.println(hdr.length + "length"); 
     msg.getHttpost().abort(); 
     System.out.println(msg.getHttpost().isAborted()); 

    } 

} 

共享对象

public class Message { 

    HttpPost httpost; 

    public HttpPost getHttpost() { 
     return httpost; 
    } 

    public void setHttpost(HttpPost httpost) { 
     this.httpost = httpost; 
    } 
} 

的Util类

public class Util { 
    private final String USER_AGENT = "Mozilla/5.0"; 

    public void print(Message msg) { 
     String url = "https://selfsolve.apple.com/wcResults.do"; 
     HttpPost post = new HttpPost(url); 
     HttpClient client = HttpClientBuilder.create().build(); 
     post.setHeader("User-Agent", USER_AGENT); 

     List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); 
     urlParameters.add(new BasicNameValuePair("sn", "C02G8416DRJM")); 
     urlParameters.add(new BasicNameValuePair("cn", "")); 
     urlParameters.add(new BasicNameValuePair("locale", "")); 
     urlParameters.add(new BasicNameValuePair("caller", "")); 
     urlParameters.add(new BasicNameValuePair("num", "12345")); 

     try { 
      post.setEntity(new UrlEncodedFormEntity(urlParameters)); 
      msg.setHttpost(post); 
      HttpResponse response = client.execute(post); 
      System.out.println("Response Code : " + response.getStatusLine().getStatusCode()); 

      BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 

      StringBuffer result = new StringBuffer(); 
      String line = ""; 
      while ((line = rd.readLine()) != null) { 
       result.append(line); 
      } 
     } catch (UnsupportedEncodingException e) { 

     } catch (IOException e) { 
      e.printStackTrace() 
     } 
    } 

主类

public class App 
{ 
    public static void main(String[] args) 
    { 
     Message msg = new Message(); 
     Util util = new Util(); 

     Thread reqThread = new Thread(new RequestThread(msg, util)); 
     Thread cancelThread = new Thread(new CancelThread(msg, util)); 

     System.out.println("Starting Threads"); 
     reqThread.start(); 
     try { 
      reqThread.sleep(2000); 
      cancelThread.start(); 
      cancelThread.join(); 
      System.out.println("closing.."); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

它的小不清洁码我同意,但它的工作原理。它会导致下面的异常并杀死线程。

java.net.SocketException: Socket is closed 
    at java.net.Socket.getInputStream(Socket.java:876) 
    at sun.security.ssl.SSLSocketImpl.doneConnect(SSLSocketImpl.java:644) 
    at sun.security.ssl.SSLSocketImpl.<init>(SSLSocketImpl.java:549) 

非常感谢所有帮助过我的人。

相关问题