Michael Preinbauer Michael Preinbauer - 17 days ago 9
Android Question

How to intercept HTTP requests made by a 3rd party library on Android?

For my project I have to use a 3rd party Java library which makes some HTTP requests to a well known server. I need to add my own header to those requests and wonder how I can realize it?

An HttpRequestInterceptor seems to be the answer, but how can I 'register' this one globally, so that the 3rd party library would use it? Is it perhaps possible using reflection?

I do not want to use a proxy app. Just my application should intercept and modify the HTTP requests of that 3rd party library.

Thanks for your help,
Michael

Answer

I need to add my own header to those requests and wonder how I can realize it?

Don't use the 3rd party Java library, and make the HTTP requests yourself. Or, remove the need for the custom header, so you can use the existing 3rd party Java library without issue.

An HttpRequestInterceptor seems to be the answer, but how can I 'register' this one globally, so that the 3rd party library would use it?

There are no such global interceptors, and it is very possible that the library is not using HttpClient, anyway -- that "interceptor" is an HttpClient concept.

Is it perhaps possible using reflection?

You would have to ask the authors of the unidentified 3rd party Java library that question.