如何在 Android GeckoView 中向请求添加额外标头?

huangapple go评论228阅读模式
英文:

How do you add extra headers into request in Android GeckoView?

问题

我在Mozilla的错误跟踪网站上找到了这个主题。看起来这个问题已经解决。然而,当我尝试在Android Studio中从Maven导入库的最新版本时,特别是版本org.mozilla.geckoview:geckoview-nightly:100.0.20220308100756

我只能使用GeckoSession.loadUri(uri)这个方法签名。期望的GeckoSession.loadUri(uri, extraHeaders)似乎找不到。有人对此有什么见解吗?我担心这里的文档相当稀缺。

英文:

I found this thread on the Mozilla bug tracking website. It seems like the issue was addressed. However, when I go to import a recent version of the library from maven in Android Studio. Specifically version org.mozilla.geckoview:geckoview-nightly:100.0.20220308100756.

The only method signature I am able to use is GeckoSession.loadUri(uri). The desired GeckoSession.loadUri(uri, extraHeaders) is nowhere to be found. Anyone have any insights into this? I am afraid that the documentation is quite sparse around here.

答案1

得分: 1

为了实现这一点,我使用了GeckoSessions的Loader,它允许您使用类似Builder的格式传递值。

为此,我会这样写:

Map <String, String> extraHeaders = Map.of("Key 1", "Value 1", "Key 2", "Value 2", 等等);

GeckoSession.Loader loader = new GeckoSession.Loader();
loader.additionalHeaders(extraHeaders);
loader.uri("https://example.com");

geckoSession.load(loader);

值得注意的是,理论上您不应该需要将每个方法分配给加载程序,而是应该使用更传统的Builder-like格式。但根据我的经验,如果您在传递之前没有在Map中声明https标头,GeckoSession似乎无法成功加载它们,所以这是最简单的方法。

英文:

To achieve this I make use of GeckoSessions' Loader, which allows you to use a Builder-like format to pass in values.

To do this, I'd write:

Map &lt;String, String&gt; extraHeaders = Map.of(&quot;Key 1&quot;, &quot;Value 1&quot;, &quot;Key 2&quot;, &quot;Value 2&quot;, etc.);

GeckoSession.Loader loader = new GeckoSession.Loader();
loader.additionalHeaders(extraHeaders);
loader.uri(&quot;https://example.com&quot;);

geckoSession.load(loader); 

Worth noting that theoretically you shouldn't need to assign each method to the loader like this and instead use a more conventional Builder-like format, but in my experience GeckoSession seems to fail at loading in https headers when you don't have them already declared in a Map before passing, so this is easiest.

huangapple
  • 本文由 发表于 2023年3月12日 15:30:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711657.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定