8377

How do I fake an specific browser client when using Java's Net library?

A small program of mine just broke because, it seems, the site I was programmatically browsing now assumes a Java request comes from a mobile phone, and the link I was looking for is not on their mobile page.

So I want to fake an Internet Explorer access. How do I do that with java.net?

Answer1:

IIRC, set "http.agent" system property through System, -D on the command line, in your JNLP file or elsewhere.

Answer2:

Assuming you're using java.net.URLConnection, then call setRequestProperty(String,String) to set the request header to a value that IE would use. For example, to fake IE6:

URL url = new URL("http://google.com"); URLConnection connection = url.openConnection(); connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; .NET CLR 1.2.30703)");

and then use the connection object as before.

But java.net is horrible. Use Apache Commons HttpClient instead, it's much nicer.

Even better, use a framework designed for navigating websites, like HtmlUnit

Answer3:

You need to set the User-Agent header in the HTTP request to a value used by Internet Explorer.

I recommend using the Jakarta HttpClient library to make the request as it provides a higher level API for manipulating the request.

Recommend

  • JNLP Webstart Launch Issue
  • Where to put application data and temp files on Windows, Linux and MAC if java-application is being
  • interactive matplotlib through eclipse
  • Android Native Window timestamp
  • javaws : Java 7 update 45. Cannot send properties [duplicate]
  • How to detect mouse scroll events if my program is not in foreground
  • Limit the number of documents in a collection in mongodb
  • Slave cannot connect to Master
  • TFS Builds: Running the builds as administrator
  • JENKINS: ERROR when I try to use an older JDK for a specific maven project
  • Varnish/Apache Random 503 Errors
  • how to save the state in userdefaults of accessory checkmark-iphone
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • VSO Build — Response status code does not indicate success: 404 (Not Found)
  • Database structure design with variable amounts of fields
  • Lost migrations and Azure database is now out of sync
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • Using $this when not in object context
  • Jenkins: How To Build multiple projects from a TFS repository?
  • How reduce the height of an mschart by breaking up the y-axis
  • Sony Xperia Z Tablet not found by adb
  • How to get a value (ex: baseURL) in every Karate feature?
  • Validaiting emails with Net.Mail MailAddress
  • Perl system calls when running as another user using sudo
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Apache 2.4 - remove | delete | uninstall
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • bootstrap to use multiple ng-app
  • How to get icons for entities from eclipse?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • Turn off referential integrity in Derby? is it possible?
  • How does Linux kernel interrupt the application?
  • Qt: Run a script BEFORE make
  • JaxB to read class hierarchy
  • Checking variable from a different class in C#
  • reshape alternating columns in less time and using less memory