Page 1 of 1

Modify request and response headers

PostPosted: Tue Mar 16, 2021 3:30 am
by ElAndLivia
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);
}

Re: Modify request and response headers

PostPosted: Tue Mar 16, 2021 9:25 am
by magreenblatt
The response can be modified in CefResourceRequestHandler.onResourceResponse.

Re: Modify request and response headers

PostPosted: Wed Mar 17, 2021 8:28 pm
by ElAndLivia
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!