java http interceptor

Solutions on MaxInterview for java http interceptor by the best coders in the world

showing results for - "java http interceptor"
Ida
13 Nov 2018
1import java.io.IOException;
2import org.apache.http.Header;
3import org.apache.http.HttpException;
4import org.apache.http.HttpRequest;
5import org.apache.http.HttpRequestInterceptor;
6import org.apache.http.HttpResponse;
7import org.apache.http.client.methods.HttpGet;
8import org.apache.http.impl.client.CloseableHttpClient;
9import org.apache.http.impl.client.HttpClients;
10import org.apache.http.message.BasicHeader;
11import org.apache.http.protocol.HttpContext;
12
13public class InterceptorsExample {
14 
15   public static void main(String args[]) throws Exception{
16      
17      //Creating an HttpRequestInterceptor
18      HttpRequestInterceptor requestInterceptor = new HttpRequestInterceptor() {
19         @Override
20         public void process(HttpRequest request, HttpContext context) throws
21         HttpException, IOException {
22            if(request.containsHeader("sample-header")) {
23               System.out.println("Contains header sample-header, removing it..");
24               request.removeHeaders("sample-header"); 
25            }
26            //Printing remaining list of headers
27            Header[] headers= request.getAllHeaders();
28            for (int i = 0; i<headers.length;i++) {
29               System.out.println(headers[i].getName());
30            }
31         }
32      };
33
34      //Creating a CloseableHttpClient object
35      CloseableHttpClient httpclient =
36      HttpClients.custom().addInterceptorFirst(requestInterceptor).build();
37
38      //Creating a request object
39      HttpGet httpget1 = new HttpGet("https://www.tutorialspoint.com/");
40
41      //Setting the header to it
42      httpget1.setHeader(new BasicHeader("sample-header","My first header"));
43      httpget1.setHeader(new BasicHeader("demo-header","My second header"));
44      httpget1.setHeader(new BasicHeader("test-header","My third header"));
45
46      //Executing the request
47      HttpResponse httpresponse = httpclient.execute(httpget1);
48
49      //Printing the status line
50      System.out.println(httpresponse.getStatusLine());
51   }
52}
Gabriela
02 Aug 2017
1@Override
2 public void process(HttpRequest request, HttpContext context) throws
3HttpException, IOException {
4   //Method implementation . . . . .
5};