Mimicking a browser user-agent in a request

0

The following code demonstrates how to 'forge' the user-agent string in a request , making the request seem like it originated from a browser.


/** *PUBLIC SOFTWARE * *This source code has been placed in the public domain. You can use, modify, and distribute *the source code and executable programs based on the source code. * *However, note the following: * *DISCLAIMER OF WARRANTY * * This source code is provided "as is" and without warranties as to performance * or merchantability. The author and/or distributors of this source code may * have made statements about this source code. Any such statements do not constitute * warranties and shall not be relied on by the user in deciding whether to use * this source code.This source code is provided without any express or implied * warranties whatsoever. Because of the diversity of conditions and hardware * under which this source code may be used, no warranty of fitness for a * particular purpose is offered. The user is advised to test the source code * thoroughly before relying on it. The user must assume the entire risk of * using the source code. * */ import java.io.*; import java.net.*; /** * * @author amal * version 1.0 */ public class UrlConB { public static void main(String[] args) { InputStreamReader in = null; try { String query = "web"; Socket s = new Socket("bing.com", 80); PrintStream p = new PrintStream(s.getOutputStream()); p.print("GET /search?q=" + query + " HTTP/1.0\r\n"); p.print("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/6.0\r\n"); p.print("Connection: close\r\n\r\n"); in = new InputStreamReader(s.getInputStream()); BufferedReader buffer = new BufferedReader(in); String line; while ((line = buffer.readLine()) != null) { System.out.println(line); } in.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (null != in) in.close(); } catch (IOException e) { // ignore } } } }

Read more »

0 comments:

Post a Comment

© Zone817. Powered by Blogger.