对于我的项目,我必须使用第 3 方 Java 库,它向知名服务器发出一些 HTTP 请求。我需要将自己的 header 添加到这些请求中,并想知道如何实现它?
HttpRequestInterceptor 似乎是答案,但我如何在全局范围内“注册”这个,以便第 3 方库可以使用它?是否可以使用反射?
我不想使用代理应用程序。只是我的应用程序应该拦截并修改该第三方库的 HTTP 请求。
感谢您的帮助, 迈克尔
最佳答案
I need to add my own header to those requests and wonder how I can realize it?
不要使用第 3 方 Java 库,并自行发出 HTTP 请求。或者,不再需要自定义 header ,这样您就可以毫无问题地使用现有的第 3 方 Java 库。
An HttpRequestInterceptor seems to be the answer, but how can I 'register' this one globally, so that the 3rd party library would use it?
不存在这样的全局拦截器,无论如何,该库很可能没有使用 HttpClient ——“拦截器”是一个 HttpClient 概念。
Is it perhaps possible using reflection?
您必须向身份不明的第 3 方 Java 库的作者询问这个问题。
关于java - 如何在Android上拦截第三方库发出的HTTP请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7496378/