-3

I want to develop a HTTP Proxy , The code is here ; When I run my code , I get this exception :

Started on: 9999 request for : /setwindowsagentaddr Encountered exception: java.net.MalformedURLException: no protocol: /setwindowsagentaddr

package proxy;
import java.net.*;
import java.io.*;


public static void main(String[] args) throws IOException {
    ServerSocket serverSocket = null;
    boolean listenning = true;
 //   String host="192.168.1.10";
    int port = 9999;

    try{
        port = Integer.parseInt(args[0]);
    }catch(Exception e){

    }

    try{
        serverSocket = new ServerSocket(port);

         System.out.println("Started on: " + port);
    }catch(Exception e){
//            System.err.println("Could not listen on port: " + args[0]);
        System.exit(0);
    }
    while(listenning){
    new ProxyThread(serverSocket.accept()).start();

    }
    serverSocket.close();

}

}

and the ProxyThread here :

public class ProxyThread extends Thread {
private Socket socket  = null;
private static final int BUFFER_SIZE = 32768;


public ProxyThread(Socket socket){
    super("Proxy Thread");
    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="";



        //get request from client
       // socket.getPort();


        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 get url
            if(cnt==0){
                String [] tokens = inputLine.split(" ");
           urlToCall = tokens[1];
                System.out.println("request for : "+ urlToCall);


            }
            cnt++;





            }

        BufferedReader rd = null;
        try{
               //System.out.println("sending request
    //to real server for url: "
            //        + urlToCall);
            ///////////////////////////////////
            //begin send request to server, get response from server
            URL url = new URL(urlToCall);
            URLConnection conn = url.openConnection();
            conn.setDoInput(true);
            //not doing http post
            conn.setDoOutput(false);
            System.out.println("Type is : "+ conn.getContentType());
            System.out.println("length is : "+ conn.getContentLength());
            System.out.println("allow user interaction :"+     conn.getAllowUserInteraction());
            System.out.println("content encoding : "+ conn.getContentEncoding());
            System.out.println("type is : "+conn.getContentType());

            // Get the response
            InputStream is = null;
            HttpURLConnection huc = (HttpURLConnection) conn;
              if (conn.getContentLength() > 0) {
                try {
                    is = conn.getInputStream();
                    rd = new BufferedReader(new InputStreamReader(is));
                } catch (IOException ioe) {
                    System.out.println(
            "********* IO EXCEPTION **********: " + ioe);
                }

            }

               //end send request to server, get response from server
               //begin 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();
             //end send response to client


        }catch(Exception e){
             //can redirect this to error log
            System.err.println("Encountered exception: " + e);
            //encountered error - just send nothing back, so
            //processing can continue
            out.writeBytes("");
        }

         //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 (Exception e) {
         e.printStackTrace();
    }

}

How can I fix this exceptions?

ABCD
  • 1
  • 5

1 Answers1

1

So the error isn't lying, /setwindowsagentaddr isn't a valid url. How would it know what protocol to use?

Try using something like http://localhost:8080/setwindowsagentaddr

David
  • 19,577
  • 28
  • 108
  • 128
  • I'm new with socket programming! where do I use http://localhost:8080/setwindowsagentaddr ?? Can you explain to me about this? – ABCD Jul 12 '16 at 11:06
  • Hi, it really has nothing to do with the sockets, it's simply that the url you're using is not a valid url, this is part of the standard validation of the instantiation of the URL object. – David Jul 12 '16 at 14:28
  • I set the Url to http://localhost:8080/ but I got the same exception , The main problem is that I don not know the meaning of "windowsagentadd" ! – ABCD Jul 12 '16 at 20:05