简体   繁体   English

如何拦截Android上第三方库发出的HTTP请求?

[英]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. 对于我的项目,我必须使用一个第三方Java库,该库向知名服务器发出一些HTTP请求。 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? 一个HttpRequestInterceptor似乎是答案,但是我如何在全局“注册”这个,以便第三方库使用它? 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. 只是我的应用程序应该拦截并修改该第三方库的HTTP请求。

Thanks for your help, Michael 谢谢您的帮助,迈克尔

创建自定义URLStreamHandlerFactory和URLStreamHandler并通过调用URL.setURLStreamHandlerFactory(this)从应用程序onCreate()进行设置,它将允许您在将其传递给服务器之前拦截和更改标头。

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. 不要使用第三方Java库,而是自己发出HTTP请求。 Or, remove the need for the custom header, so you can use the existing 3rd party Java library without issue. 或者,不需要自定义标头,因此您可以毫无问题地使用现有的第三方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? 一个HttpRequestInterceptor似乎是答案,但是我如何在全局“注册”这个,以便第三方库使用它?

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. 没有此类全局拦截器,并且该库很可能仍未使用HttpClient -“拦截器”是HttpClient的概念。

Is it perhaps possible using reflection? 使用反射是否可能?

You would have to ask the authors of the unidentified 3rd party Java library that question. 您将不得不向身份不明的第三方Java库的作者提问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM