0

I have a webview in my native android app and need to set the agent to fix desktop view on certain websites that check for android agent using javascript

JavaScriptCode:

if (navigator.userAgent.indexOf("Android") != -1) {
    //do something
}

on android side I have tried to set the agent as follow

public void setDesktopMode(WebView webView, boolean enabled) {
    String newUserAgent = webView.getSettings().getUserAgentString();
    if (enabled) {
        try {
            String ua = webView.getSettings().getUserAgentString();
            String androidOSString = webView.getSettings().getUserAgentString().substring(ua.indexOf("("), ua.indexOf(")") + 1);
            newUserAgent = webView.getSettings().getUserAgentString().replace(androidOSString, "(X11; Linux x86_64)");
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        newUserAgent = null;
    }

    webView.getSettings().setUserAgentString(newUserAgent);
    webView.getSettings().setUseWideViewPort(enabled);
    webView.getSettings().setLoadWithOverviewMode(enabled);
    webView.reload();
}

however the website has some redirects to external sites. after logging the header from WebView I have noticed that in some of these redirects the agent is set as android which causes some issues on the layout and website behavior.

WebView Request Header Log:

request.getRequestHeaders()::{Origin=null, User-Agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/69.0.3497.100 Mobile Safari/537.36, Accept=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8, Upgrade-Insecure-Requests=1, Content-Type=application/x-www-form-urlencoded}
request.getMethod()::POST
request.getUrl()::https://www.example.com/tt.jsp
request.getRequestHeaders()::{Origin=https://www.example.com, User-Agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/69.0.3497.100 Mobile Safari/537.36, Referer=https://www.example.com/tt.jsp, Accept=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8, Upgrade-Insecure-Requests=1, Content-Type=application/x-www-form-urlencoded}
request.getMethod()::POST
request.getUrl()::https://www.example2.com/tt2.do
request.getRequestHeaders()::{User-Agent=Mozilla/5.0 (Linux; Android 8.0.0; SM-G955F Build/R16NW; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/69.0.3497.100 Mobile Safari/537.36, Accept=image/webp,image/apng,image/*,*/*;q=0.8, Referer=https://www.example.com/tt.jsp}
request.getMethod()::GET
request.getUrl()::https://www.example.com/favicon.ico
request.getRequestHeaders()::{User-Agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/69.0.3497.100 Mobile Safari/537.36, Accept=*/*, Referer=https://www.example2.com/}
request.getRequestHeaders()::{User-Agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/69.0.3497.100 Mobile Safari/537.36, Accept=text/css,*/*;q=0.1, Referer=https://www.example2.com/}
request.getMethod()::GET
request.getRequestHeaders()::{User-Agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/69.0.3497.100 Mobile Safari/537.36, Accept=text/css,*/*;q=0.1, Referer=https://www.example2.com/}
request.getUrl()::https://www.example2.com/css.css

How can I ensure that the Webview agent does not showAndroid as OS in User-Agent? as you can see in one of the redirects shown in logs it shows android as user-agent OS

UPDATE: I have tried using webView.getSettings().setUserAgentString("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/69.0.3497.100 Mobile Safari/537.36"); still produces same result as shown in the logs above. (log line 7)

Poorya
  • 1,291
  • 6
  • 27
  • 57
  • Possible duplicate of [android user agent](https://stackoverflow.com/questions/5586197/android-user-agent) – Martin Zeitler Sep 26 '18 at 02:55
  • @MartinZeitler I have tried that and also produced similar results. the odd behavior is in the logs. line 7 – Poorya Sep 26 '18 at 03:03
  • you might also have to set the request-headers; either by override or by interception... when you say redirects; this might be whenever receiving a `301` header. you'd need to identify the situation first. – Martin Zeitler Sep 26 '18 at 03:06
  • I have tried to do that but didn't manage to successfully override the header using `shouldinterceptrequest`, – Poorya Sep 26 '18 at 03:08
  • @MartinZeitler can you guide me on how to identify that ? – Poorya Sep 26 '18 at 03:09
  • this should catch all requests: https://stackoverflow.com/a/29811280/549372 – Martin Zeitler Sep 26 '18 at 03:12
  • @MartinZeitler i tried the solution in the link you provided, for some reason it crashed the webview. but meanwhile it helped me identify 301. i noticed this particular redirect opens the redirect link in browser if i remove the `shouldinterceptrequest`. however I need to open the page in webview while setting the making sure the user agent does not contain `Android` – Poorya Sep 26 '18 at 03:53

0 Answers0