Redirecting an HTTP request to another server

I am trying to implement a fail-safe system. So I created a monitoring server that presumably redirects the request to another application server that is alive.

So far I have developed a small monitoring server and tried to redirect the request using a status code HTTP/1.1 302 Found\r\n

. But I cannot redirect to my VM, although I can access it directly in my browser by typing in the URL.

Routing code -

class Connection extends Thread{
    Socket clientSocket;
    BufferedReader din;
    OutputStreamWriter outWriter;

    public Connection(Socket clientSocket){
        try{
            this.clientSocket = clientSocket;
            din = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), "ASCII"));
            outWriter = new OutputStreamWriter(clientSocket.getOutputStream());
            this.start();
        }catch(IOException e){
            System.out.println("Connection: " + e.getMessage());
        }   
    }
    public void run(){
        try{
        String line = null;
        while((line = din.readLine())!=null){
            System.out.println("Read" + line);
            if(line.length()==0)    
                break;
        }
        //here write the content type etc details:
        System.out.println("Someone connected: " + clientSocket);

        }catch(EOFException e){
            System.out.println("EOF: " + e.getMessage());
        }
        catch(IOException e){
            System.out.println("IO at run: " + e.getMessage());
        }finally{
            try{
                routeRequest(outWriter);
                outWriter.close();
                clientSocket.close();
            }catch(IOException e){
                System.out.println("Unable to close the socket");
            }
        }
    }

    public void routeRequest(OutputStreamWriter outWriter){
        try {
            outWriter.write("HTTP/1.1 302 Found\r\n");
            outWriter.write("Date: Tue, 11 Jan 2011 13:09:20 GMT\r\n");         
            outWriter.write("Content-type: text/plain\r\n");
            outWriter.write("Server: vinit\r\n");           
            outWriter.write("Location: http://192.168.74.128:8080/Stateless/index.html");
            outWriter.write("Connection: Close");
            outWriter.write("\r\n\r\n");
//          String responseStr = "<html><head><title>Hello</title></head><body>Hello world from my server</body></html>\r\n";
//          outWriter.write(responseStr);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

      

But in the browser, instead of routing, I get a not received data page.

Please let me know if I am doing something wrong.

PS: I can display a dummy home page hello if I go with a 200 status

Thanks in advance.

+3


source to share


1 answer


There is Location:

no end-of-line indicator in the header .

outWriter.write("Location: http://192.168.74.128:8080/Stateless/index.html\r\n");

      

Failure to do so will result in the header Connection:

becoming part of the redirected URL. The client will remain connected to the server unless your server is explicitly closed after delivering this response.



Even if the client expires and closes the connection, it might consider the URL invalid. If it redirects, the page will not be found as the path will look something like this:

/Stateless/index.htmlConnection: Close

+3


source







All Articles