Modify request and response headers

Having problems with building or using the JCEF Java binding? Ask your questions here.

Modify request and response headers

Postby ElAndLivia » Tue Mar 16, 2021 3:30 am

I'm trying to modify the request and response headers during the normal network transaction. I've found it possible to modify request headers via onBeforeResourceLoad, but I'm not sure if jcef can modify response headers. Could anyone tell me how to modify them?

Here is my code to modify request headers. If I have made any mistakes please tell me, thank you!
Code: Select all
public void modifyRequestHeaders() {
   class JcefRequestHandler extends CefRequestHandlerAdapter{
      @Override
      public boolean onBeforeResourceLoad(CefBrowser browser, CefFrame frame, CefRequest request) {
         if (request.getURL().indexOf("islocal=true") > 0) {
            HashMap<String, String> headerMap = new HashMap<String, String>();
            request.getHeaderMap(headerMap);
              String key = null, value = null;
              for (Map.Entry<String, String> header : headerMap.entrySet()) {
                 if (header.getKey().toLowerCase().contains("cookies")) {
                    key = header.getKey();
                    value = header.getValue();
                 }
              }
              if (key != null) {
                 headerMap.put("Cookie", value);
              }
              headerMap.remove(key);
              request.setHeaderMap(headerMap);
         }
         return false;
      }
   }
      
   JcefRequestHandler requestHandler = new JcefRequestHandler();
   client.addRequestHandler(requestHandler);
}
ElAndLivia
Newbie
 
Posts: 2
Joined: Mon Mar 15, 2021 2:57 pm

Re: Modify request and response headers

Postby magreenblatt » Tue Mar 16, 2021 9:25 am

The response can be modified in CefResourceRequestHandler.onResourceResponse.
magreenblatt
Site Admin
 
Posts: 11093
Joined: Fri May 29, 2009 6:57 pm

Re: Modify request and response headers

Postby ElAndLivia » Wed Mar 17, 2021 8:28 pm

magreenblatt wrote:The response can be modified in CefResourceRequestHandler.onResourceResponse.

Thank you for your reply!
But I'm still confused about CefResourceRequestHandler.onResourceResponse's parameter declaration:
response : The request response. Cannot be modified in this callback. Instance only valid within the scope of this method.

I've tried to modify the response in the method like this:
Code: Select all
class RequestHandler extends CefResourceRequestHandlerAdapter implements CefRequestHandler{
        @Override
        public boolean onResourceResponse(
                CefBrowser browser, CefFrame frame, CefRequest request, CefResponse response) {
              HashMap<String, String> headerMap = new HashMap<String, String>();
              response.getHeaderMap(headerMap);
              /* modify headerMap */
              response.setHeaderMap(headerMap);
           }
           return false;
        }

   @Override
   public CefResourceRequestHandler getResourceRequestHandler(CefBrowser browser, CefFrame frame,
      CefRequest request, boolean isNavigation, boolean isDownload, String requestInitiator,
         BoolRef disableDefaultHandling) {
      return this;
   }

   /* other methods */
}
client.addRequestHandler(new RequestHandler());

But if I create a new headerMap and execute response.getHeaderMap(headerMap) again, the headerMap seems to be just the same as before without any change. Am I using the onResourceResponse method in a wrong way? Thank you!
ElAndLivia
Newbie
 
Posts: 2
Joined: Mon Mar 15, 2021 2:57 pm


Return to JCEF Forum

Who is online

Users browsing this forum: No registered users and 2 guests