Apache HttpClient-요청 중단

다음을 사용하여 현재 HTTP 요청을 중단 할 수 있습니다. abort() 즉,이 메서드를 호출 한 후 특정 요청에 대해 실행이 중단됩니다.

한 번 실행 한 후에이 메서드를 호출하면 해당 실행의 응답이 영향을받지 않고 후속 실행이 중단됩니다.

예

다음 예제를 살펴보면 HttpGet 요청을 생성하고 다음을 사용하여 요청 형식을 인쇄했습니다. getMethod().

그런 다음 동일한 요청으로 다른 실행을 수행했습니다. 다시 첫 번째 실행을 사용하여 상태 줄을 인쇄 했습니다. 마지막으로 두 번째 실행의 상태 줄을 인쇄했습니다.

논의 된 바와 같이, 첫 번째 실행 (중단 메소드 전 실행) 의 응답 이 인쇄되고 (중단 메소드 이후에 기록 된 두 번째 상태 행 포함), abort 메소드 이후 현재 요청의 모든 후속 실행이 실패합니다. 예외.

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

public class HttpGetExample {
   public static void main(String args[]) throws Exception{
   
      //Creating an HttpClient object
      CloseableHttpClient httpclient = HttpClients.createDefault();

      //Creating an HttpGet object
      HttpGet httpget = new HttpGet("http://www.tutorialspoint.com/");

      //Printing the method used
      System.out.println(httpget.getMethod());
 
      //Executing the Get request
      HttpResponse httpresponse = httpclient.execute(httpget);

      //Printing the status line
      System.out.println(httpresponse.getStatusLine());

      httpget.abort();
      System.out.println(httpresponse.getEntity().getContentLength());
 
      //Executing the Get request
      HttpResponse httpresponse2 = httpclient.execute(httpget);
      System.out.println(httpresponse2.getStatusLine());
   }
}

산출

실행시 위의 프로그램은 다음과 같은 출력을 생성합니다.

On executing, the above program generates the following output.
GET
HTTP/1.1 200 OK
-1
Exception in thread "main" org.apache.http.impl.execchain.RequestAbortedException:
Request aborted
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:180)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
at HttpGetExample.main(HttpGetExample.java:32)