Adding custom headers on all browsers
Asked Answered
S

2

4

I am working on a application where i want to add headers to browser in android. Its working pretty fine on Google chrome.

But this is not working on other available browsers like Firefox, UC browser, OperaMini, Dolphin

Below is the code that i tried.

Intent mIntent = new Intent(Intent.ACTION_VIEW,Uri.parse(URL));
Bundle bundle = new Bundle();
bundle.putString(Constants.REQUEST_HEADER_TOKEN, "token");
bundle.putString(Constants.REQUEST_HEADER_AUTH, "Basic bfjdslfs");
mIntent.putExtra(Browser.EXTRA_HEADERS, bundle);
startActivity(mIntent);

Any help will be appreciated.Thanks in advance.

Spectrograph answered 18/7, 2016 at 13:31 Comment(4)
what have u tried so far other than this...?? please mention few links atleast...also what error you get while adding headers to other browsers.Leslileslie
why don't you use web view....??Aphra
Exactly,,what kiran is saying is right...!! use webviewLeslileslie
@HarrySharma, WebView is often a bad solution, because it differs from OS to OS and manufacturer to manufacturer. Mobile browsers are much better than WebView.Bukharin
M
4

There is no requirement that browsers pay any attention to extras like EXTRA_HEADERS, REQUEST_HEADER_TOKEN, etc.

Either use WebView or live without the headers always being added.

Meza answered 18/7, 2016 at 13:35 Comment(3)
@PragyaSingla: WebView can handle server-side redirects, when using a WebViewClient. Since you have no means of changing the behavior of other Web browsers, you need to change your approach.Meza
In web-view sometimes redirection link gets broken in between during redirection process and there is no way to identify whether link is down or up. In this case web-view stop processing. So I have to use a web browser.Spectrograph
@PragyaSingla: Perhaps you should ask a separate Stack Overflow question for assistance with your WebView issue. After all, some Web browsers use WebView. Or, change your Web server such that you do not need these custom headers. Or, do not use WebView to do the HTTPS request, but instead use something else (e.g., HttpURLConnection, OkHttp3). I repeat: since you have no means of changing the behavior of other Web browsers, you need to change your approach.Meza
K
0

This solution definitely works with mobile chrome browser ( haven't test ob others)

 Intent mIntent = new Intent(Intent.ACTION_VIEW,Uri.parse(URL));
    Bundle bundle = new Bundle();
    bundle.putString("Authorization", "Basic " + token);
    mIntent.putExtra(Browser.EXTRA_HEADERS, bundle);
    startActivity(mIntent);

However be careful with links for files that can be opened in some other default application.

In my case, there was problem with pdf Every link that ends with .pdf (http://lol.com/test.pdf) is opening not in web browser but in some pdf reader and then EXTRA_HEADERS aren't sent.

Kutenai answered 20/9, 2019 at 16:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.