开发者

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开发者_Python百科 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


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.


Create custom URLStreamHandlerFactory and URLStreamHandler and set it from application onCreate() by calling URL.setURLStreamHandlerFactory(this) it will allow you to intercept and change headers before pass it to server.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜