Results 1 to 2 of 2
Thread: Listening to a specific URL
- 06-20-2015, 11:56 AM #1
Member
- Join Date
- Jun 2015
- Posts
- 4
- Rep Power
- 0
Listening to a specific URL
Hi!
I have an assignment in which I need to create a program that would intercept that when a specific URL has been entered, it would load other contents instead of the content it currently has. For example if I were to load example.com and it would contain a string "Hello World", I will have to load it with "I did it!".
After investigating I found that the best approach for me would be to use a web proxy. However, I am only able to intercept localhost, and not external URLs.
I will appreciate any advice regarding catching when the correct URL has been entered.
Here is what I got so far:
Java Code:public class ProxyServer { public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; boolean listening = true; int port = 8118; serverSocket = new ServerSocket(port); System.out.println("Started on: " + port); while(listening){ Thread t = new ProxyThread(serverSocket.accept()); t.start(); } serverSocket.close(); } }
Java Code:public class ProxyThread extends Thread { private Socket socket = null; private static final int BUFFER_SIZE = 32768; public ProxyThread(Socket socket) { super("ProxyThread"); this.socket = socket; } public void run() { try { DataOutputStream out = new DataOutputStream(socket.getOutputStream()); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); String inputLine, outputLine; int cnt = 0; String urlToCall = ""; //begin get request from client while ((inputLine = in.readLine()) != null) { try { StringTokenizer tok = new StringTokenizer(inputLine); tok.nextToken(); } catch (Exception e) { break; } //parse the first line of the request to find the url if (cnt == 0) { String[] tokens = inputLine.split(" "); urlToCall = tokens[1]; System.out.println("Request for : " + urlToCall); } cnt++; } BufferedReader rd = null; try { URL url = new URL(urlToCall); URLConnection conn = url.openConnection(); String line = "I did it!"; InputStream is = null; is = new ByteArrayInputStream(line.getBytes(Charset.forName("UTF-8"))); rd = new BufferedReader(new InputStreamReader(is)); //send response to client byte by[] = new byte[ BUFFER_SIZE ]; int index = is.read( by, 0, BUFFER_SIZE ); while ( index != -1 ) { out.write( by, 0, index ); index = is.read( by, 0, BUFFER_SIZE ); } out.flush(); } catch (Exception e) { System.err.println("Encountered exception: " + e); } //close out all resources if (rd != null) { rd.close(); } if (out != null) { out.close(); } if (in != null) { in.close(); } if (socket != null) { socket.close(); } } catch (IOException e) { e.printStackTrace(); } } }
- 06-20-2015, 03:56 PM #2
Similar Threads
-
Check a specific value from a specific line from .txt file
By maki in forum New To JavaReplies: 14Last Post: 02-09-2015, 03:08 AM -
Listening for variables?
By martypapa in forum New To JavaReplies: 6Last Post: 02-09-2010, 10:14 AM -
MDB listening
By nipunalk in forum Advanced JavaReplies: 5Last Post: 01-20-2010, 06:10 AM -
how do i print a specific txt file on a specific printer
By nikhilbhat in forum New To JavaReplies: 2Last Post: 11-08-2008, 11:40 AM
Bookmarks