How can I identify whether the browser is Firefox or Chrome? Basically, I want an application to run only on the specific browser that is registered by a user. For this scenario, I want my application to identify the browser which the user is using, to know whether the application is permitted to run.
I am using java servlet.
I tried a the browser’s local storage, but it can be deleted with no control from my application. If local storage can be used, please let me know how.
(Yes I can get a browser info, but I want to identify a specific machine with a browser from where my application user is permitted to run the application; otherwise, I need to restrict that user from accessing my application.)
Fetch user-agent properties from the HTTP requeste header.
String userAgent=req.getHeader("user-agent");
String browserName = "";
String browserVer = "";
if(userAgent.contains("Chrome")){ //checking if Chrome
String substring=userAgent.substring(userAgent.indexOf("Chrome")).split(" ")[0];
browserName=substring.split("/")[0];
browserVer=substring.split("/")[1];
}
else if(userAgent.contains("Firefox")){ //Checking if Firefox
String substring=userAgent.substring(userAgent.indexOf("Firefox")).split(" ")[0];
browserName=substring.split("/")[0];
browserVer=substring.split("/")[1];
}
httpRequest.getHeader("user-agent")
Please use the below code in servlet to know what browser is hitting your servlet.
String userAgent = request.getHeader("user-agent");
public class MyServlet extends HttpServlet
{
#Override
public void doGet(final HttpServletRequest aRequest,
final HttpServletResponse aResponse) throws ServletException, IOException
{
final String agent = aRequest.getHeader("user-agent");
// agent will looks like
// Mozilla/5.0 (Windows NT 5.1; rv:23.0) Gecko/20100101 Firefox/23.0
// Mozilla/2.0 (compatible; MSIE 6.0; Windows NT 5.2)
// Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
// etc.
}
}
Here is the code :
String userAgent = req.getHeader("user-agent");
UserAgent ua = UserAgent.parseUserAgentString(userAgent);
Version browserVersion = ua.getBrowserVersion();
String browserName = ua.getBrowser().toString();
int majVersion = Integer.parseInt(browserVersion.getMajorVersion());
Or use can easily get the browser deatils from javascript code like this -
Browser CodeName = navigator.appCodeName
Browser Name = navigator.appName
Browser Version = navigator.appVersion
Related
Is there any way to exactly implement a "desktop mode" function into my mobile app when loading website content? I'm making a android app, and I want to have a page just with static website content just like in a web browser, I really like how mobile opera implemented this feature, So I just want to know if there is a way to do that. And if there is, then How?
(I'm making this project in Java and viewing the website using WebView from the library "WebKit")
I've also tried changing the User Agent, which didn't work on a static website.
This method helps you to set DesktopMode on webview
public void setDesktopMode(WebView webView,boolean enabled) {
String newUserAgent = webView.getSettings().getUserAgentString();
if (enabled) {
newUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5)\nAppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/85\nVersion/11.1.1 Safari/605.1.15";
}
webView.getSettings().setUserAgentString(newUserAgent);
webView.getSettings().setUseWideViewPort(enabled);
webView.getSettings().setLoadWithOverviewMode(enabled);
webView.reload();
}
Call it like that
Mobile mode : setDesktopMode(webView, false);
Desktop mode : setDesktopMode(webView, true);
For Kotlin:
fun setDesktopMode(webView: WebView, enabled: Boolean) {
var newUserAgent: String? = webView.settings.userAgentString
if (enabled) {
newUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5)\nAppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/85\nVersion/11.1.1 Safari/605.1.15"
}
webView.settings.apply {
userAgentString = newUserAgent
useWideViewPort = enabled
loadWithOverviewMode = enabled
}
webView.reload()
}
I would like to use HtmlUnit to login to website and click a link so that a file would be downloaded, however, the website, which uses JQuery, returns a "Browser Not Supported" Error. Is there a way that HtmlUnit can be set to look exactly like a normal browser to this website?
Any help would be greatly appreciated.
I'm trying to do this with the following settings, but the error is still occurring:
public void surf(Job job) {
System.out.println("[Enter] surf");
try {
String applicationName = "Netscape";
String applicationVersion = "5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36 OPR/38.0.2220.41";
String userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36 OPR/38.0.2220.41";
int browserVersionNumeric = 51;
BrowserVersion browser = new BrowserVersion(applicationName, applicationVersion, userAgent, browserVersionNumeric);
WebClient webClient = new WebClient(browser);
final HtmlPage page = webClient.getPage("https://www.europasports.com");
System.out.println(page);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("[Exit] surf");
}
Netscape was discontinued in March 2008, that's why you are getting the message. It no longer exists! If you are targeting Apple, i suggest you use Safari, but Google Chrome currently has the largest browser usage share
I want to read mobile version of the website but my program reads the normal website.
I am using this property
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
What am I supposed to do?
The decision on which page to serve by the server is made based upon the "User-Agent" property of the request.
To get the mobile version of the page, take a look at this chrome dev article detailing chrome on android user agent strings, and set the "User-Agent" string in your header to be that of a mobile client; it doesn't look like the User-Agent string you have used in your question is that of a mobile client.
For example,
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
String userAgent = "Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19";
try {
httppost.setHeader("User-Agent", userAgent);
// Add your data
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
// ....
} catch ... {
This should give you the mobile version of a page, as would be seen by a Galaxy Nexus device.
Here is a list of a ton of mobile browser user agent strings: http://www.useragentstring.com/pages/Mobile%20Browserlist/
Maybe try a different user agent string like:
Opera/9.80 (J2ME/MIDP; Opera Mini/9 (Compatible; MSIE:9.0; iPhone; BlackBerry9700; AppleWebKit/24.746; U; en) Presto/2.5.25 Version/10.54
If I request the following URL
http://www.google.com/recaptcha/api/noscript?k=MYPUBLICKEY
I will get old no-script version of captcha, containing image of Google street number, like this
But if I'll do the same with HtmlUnit I will get some faked version of image, like this:
It happens all the time: real-world street number from browser and blackish distorted text from HtmlUnit. Public key is the same.
How can Google server distinguish between browser and HtmlUnit?
The HtmlUnit code is follows:
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_17);
final HtmlPage page = webClient.getPage("http://www.google.com/recaptcha/api/noscript?k=" + getPublicKey());
HtmlImage image = page.<HtmlImage>getFirstByXPath("//img");
ImageReader imageReader = image.getImageReader();
Process is observable with Fiddler.
And how about setting correct Headers for your request? User-Agent is a key here.
Headers are the way that backend can get client information (Firefox, Chrome etc) and what is it in your case? Set correct headers eg. for Firefox:
conn.setRequestProperty("User-Agent", " Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0.1) Gecko/20100101 Firefox/8.0.1");
conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
This snipped if from my code using Apache HttpClient, you need to adapt it to your needs.
I know this is old post but, good way is to use
WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER);
How you solve your problem?
I am executing following sample program of httpclient of "GET" method.
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class TestMethodStatuscode {
public static void main(String[] args) throws Exception
{
HttpClient client = new HttpClient();
client.getParams().setParameter(HttpMethodParams.USER_AGENT,
"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729)");
//client.getParams().setCookiePolicy(org.apache.http.client.params.CookiePolicy.BROWSER_COMPATIBILITY);
GetMethod get = new GetMethod("http://de.mg40.mail.yahoo.com/neo/launch?.rand=80g4u84m26ifl");
//get_siteurl.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
client.executeMethod(get);
System.out.println("Status code: "+get.getStatusCode());
//System.out.println(get.getResponseBodyAsString());
get.releaseConnection();
}
}
output:- Status code: 200
The url I am trying to fetch is some url which I get during process of login to yahoo.de email account (login to yahoo.de did not work for me so was trying this code). If I enable wireshark (filter-http or (http.request.method == POST or http.request.method == GET) and then type this url in browser , press enter and finally I notice in wireshark that the return code of the above url is 302 which means it is redirected.
Also when I run my program and check in wireshark, I see that method returns the code 302. So my queston is why it is giving me 200 as a statuscode as output and not 302 ?
As per documentation:
GetMethods will follow redirect requests from the http server by default. This behavour can be disabled by calling setFollowRedirects(false).
You probably follow redirects set to true. You can get this with the getFollowRedirects() method. If that returns true, it will automatically follow redirects. You can set it to false to remove that behavior.