Results 1 to 2 of 2
  1. #1
    madness123 is offline Member
    Join Date
    Jun 2015
    Posts
    4
    Rep Power
    0

    Default 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();
            }
        }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Listening to a specific URL

    All seems rather underhand to me. Why would you want to do this?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 14
    Last Post: 02-09-2015, 02:08 AM
  2. Listening for variables?
    By martypapa in forum New To Java
    Replies: 6
    Last Post: 02-09-2010, 09:14 AM
  3. MDB listening
    By nipunalk in forum Advanced Java
    Replies: 5
    Last Post: 01-20-2010, 05:10 AM
  4. Replies: 2
    Last Post: 11-08-2008, 10:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •