I try to get webpage using HttpURLConnection, and get a result of mobile version page.
Now I want to get a desktop version page, and try to use: System.setProperty("http.agent", "Mozilla/5.0 (Windows NT 6.1; rv:7.0.1) Gecko/20100101 Firefox/7.0"); or setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:7.0.1) Gecko/20100101 Firefox/7.0"); But I still get a result of mobile version page.
Does anyone know how to get a desktop version source code of webpage instead the mobile version?
PS: My Android is 2.3.7
You have to set the User-Agent of your connection to pretend that it's a desktop client.
As you are using the HttpUrlConnection, you can do something like:
URL url = new URL( "http://www.google.co.in/" );
HttpUrlConnection connection = (HttpUrlConnection) url.openConnection();
connection.setRequestProperty( "User-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64)
Related
I am trying to get response of website using JSoup.
I am able to get the response of website using JSoup as follows, it returns the desktop website's response
String str = "http://hplus.com.vn/xem-tivi-htv9-full-hd-1080-52834.html";
doc = Jsoup.connect(str)
.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36")
.get();
Same way, I am trying to get the response for mobile version to this same site as follows,
doc = Jsoup.connect("http://hplus.com.vn/xem-tivi-htv9-full-hd-1080-52834.html")
.userAgent("Mozilla/5.0(Linux; U; Android 4.2; en-gb; LG-P500 Build/FRF91) AppleWebKit/533.0 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1")
.get();
But this gives only desktop/laptop version response and not the mobile response.
Picture Format web :
Picture Format mobile :
How to get the mobile response from jsoup.
Thanks in advance.
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);
I've been looking for a way to change the user agent in my PhantomJSDriver and haven't found one so far. There's a blurb in the Selenium docs about the Firefox driver but I'm using PhantomJS. Any ideas?
The user agent cannot be directly set on the driver. Specifically, the driver looks for the "phantomjs.page.settings.userAgent" property, so it has to be set before invoking the driver.
Here is an example of a user agent that approximates the capabilities of PhantomJS 1.x best:
String userAgent = "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1";
System.setProperty("phantomjs.page.settings.userAgent", userAgent);
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "userAgent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36");
new PhantomJSDriver(dc);
With the recent PhantomJSDriver, a better approach is setting the user agent via capabilities:
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0");
WebDriver driver = new PhantomJSDriver(caps);
Setting the property via System.setProperty() appears to be no longer working.
I would like to know if is possible to identify (with JAVA) the kind of computer used to make a request, for example: Server, desktop, PDA (tablet,cellphone,etc)?
Thank you!
Depends on what are you using to accept requests. For http requests, informations are in User agent section of request header.
Yes it is to a degree. You have to get the User-Agent string from the HTTP request. How to do that will depend on your Java and framework implementation but that's the direction you should take. You will have to examin the string for browser versions, mobile, etc...
Here is the request from my Mac:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/536.30.1 (KHTML, like Gecko) Version/6.0.5 Safari/536.30.1
And here from my Windows server:
Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
And here from my iPhone:
Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_3 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10B329 Safari/8536.25
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.