Unable to get `User-Agent` properly from header - java

`Hi,
I am trying to get the user's browser information in my servlet filter. I used a simple code, see below.
String userAgent = request.getHeader("User-Agent");
User was using Google chrome, and what the above code printed is below.
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36
It printed the names of all the major browsers instead of getting the once the app is running. what is wrong here?

Nothing is wrong here.
For example let us consider the returned string is
Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36
Then the explanation is
ChromeChrome 41.0.2228.0
Mozilla ==>
MozillaProductSlice. Claims to be a Mozilla based user agent, which is only true for Gecko browsers like Firefox and Netscape. For all other user agents it means 'Mozilla-compatible'. In modern browsers, this is only used for historical reasons. It has no real meaning anymore
5.0 ==> Mozilla version
Windows NT 6.1 ==> Operating System Windows 7
AppleWebKit ==> The Web Kit provides a set of core classes to display web content in windows
537.36 ==> Web Kit build
KHTML ==> Open Source HTML layout engine developed by the KDE project
like Gecko ==> like Gecko...
Chrome Name ==> Chrome
41.0.2228.0 ==> Chrome version
Safari ==> Based on Safari
537.36 ==> Safari build
Description: Free open-source web browser developed by Google.
Chromium is the name of the open source project behind Google Chrome, released under the BSD license.
You can find more information in below link
http://www.useragentstring.com/pages/Chrome/
click on each link on the page to get more info

Related

How do I access WhatsAppWeb in an Android WebView?

I want to use WhatsAppWeb (web.whatsapp.com) in an Android app. I've tried using a WebView and load the URL but I'm redirected to the regular WhatsApp page (www.whatsapp.com). I tried changing the User Agent
mWebView.getSettings().SetUserAgentString("Mozilla / 5.0 (Windows NT 10.0; Win64; x64) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / 78.0.3945.79 Safari / 537.36");
but this only works if the app is started the first time. Each further start it redirects. Deleting Cache works (no redirect) but then I have to "authenticate" the device again.
Any ideas?
Did you try using custom webview client? Please check this http://www.technotalkative.com/android-webviewclient-example/

Android Studio Webview with Chrome?

I want to show a website in Android Studio with the web-view. The Website doesn´t load but there is a message:
TO use this site: Please use a standard-compliants web browser such as Safari 4.0+, Chrome or Firefox 15.0+, JavaScript must be enabled to use this site.
And I have enabled Javascript.
Do you know what I can do that the website loads anyway?
WebView w = (WebView) findViewById(R.id.w);
WebSettings ws = w.getSettings();
ws.setJavaScriptEnabled(true);
w.loadUrl("https://www.supremenewyork.com/mobile/");
You can try to add user-agent string like this :
ws.setUserAgentString("Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36");
You can get a list of user agents here :
https://developers.whatismybrowser.com/useragents/explore/

Perform a search using JSoup

Since the Soundcloud Java API is discontinued, I want to perform a search on their site using JSoup. I am currently using this code:
Document doc = Jsoup
.connect("https://soundcloud.com/search?q=deep%20house")
.userAgent("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36")
.timeout(5000).get();
But the webpage is giving me a message that I should be using a newer browser:
<p class="messageText sc-text-light">Your current browser isn't compatible with SoundCloud.<br>Please download one of our supported browsers. Need help?</p>
I have tried using other user agents which I found here but none seems to work so far. What can I do to prevent this message from popping up?
You can try the below snippet. User agent string taken from this thread.
Document doc = Jsoup
.connect("https://soundcloud.com/search?q=deep%20house")
.userAgent("Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19")
.timeout(5000).get();
System.out.println(doc);

Displaying Mobile WordPress in Android

I'm currently working on an app for a website/class. Currently, it pulls the RSS feed of the site, parses it appropriately, and when an item is clicked, it launches a new Activity with a WebView to display the page.
My current problem is that on smaller devices, such as the HTC One or Samsung Stratosphere, it defaults to showing the mobile version of the site. This is precisely what I want to happen across all devices, but on my Samsung Galaxy Note 8, it defaults to loading the desktop version of the page.
Is anyone familiar with WordPress and Android and can help me force the WebView on the Galaxy (and, therefore, other devices) to load the mobile page by default?
Thank you!
Use WebSettings object obtained by the WebView's getSettings() Method and change the UserAgent string.
WebSetting settings = webView.getSettings();
settings.setUserAgentString("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0");
For current Firefox 26.0 or browser of your choice. This should force the Website to display in Desktop mode, since this is what the Websites do to determine if they serve a mobile or desktop version.
Update:
You an also force it mobile by using mobile UserAgent. For more information check out this website with example UserAgents.
Some examples for historical references if the link becomes invalid:
Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30
Mozilla/5.0 (Linux; U; Android 4.0.3; de-ch; HTC Sensation Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30
http://www.useragentstring.com/pages/useragentstring.php
WebSettings

Guessing what is the client browser

I got the following information using request.getHeader("User-Agent") method inside a Servlet:
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10
Actually what is the client browser?
It's Chrome 8.0.552.
This website may be useful for future consultations: http://user-agent-string.info. Paste the UA string there and click Analyze. They have even a XML-RPC webservice.

Categories

Resources