Sending web request and not waiting for response

2019-07-30 15:44发布

问题:

I am using this way of sending http get requests at the moment using HttpURLConnection. (http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/)

I was wondering how would I go about not receiving the response from the request or even acknowledge it at all and just needed the request to be fired off as fast as possible how could I achieve this?

Thanks.

回答1:

What you need is a "fire and forget" semantic, not directly related to http request, rather more to threads. Simply launch whatever your task is, in a new thread

Thread thread = new Thread(new Runnable(){
  @Override
  public void run(){
    HttpURLConnectionExample http = new HttpURLConnectionExample();
    System.out.println("Testing 1 - Send Http GET request");
    http.sendGet();
  }
});
thread.start();