2016-07-12 6 views
-3

HTTPプロキシを開発したい、 コードはここにあります。 私は私のコードを実行すると、私はこの例外を取得:上の開始例外:java.net.MalformedURLException:プロトコルなし:/ setwindowsagentaddr

:9999のための 要求:/ setwindowsagentaddr が発生しました例外:java.net.MalformedURLException:なしプロトコル:/ 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(); 

} 

}

、ここProxyThread:

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

} 

どのように私はこの問題を解決することができ例外?

答えて

0

エラーが存在しないため、/ setwindowsagentaddrは有効なURLではありません。どんなプロトコルを使うべきか、どのように分かっていますか?

は、私はソケットプログラミングに新たなんだhttp://localhost:8080/setwindowsagentaddr

+0

のようなものを使用してみてください! どこでhttp:// localhost:8080/setwindowsagentaddr を使用しますか? これについて私に説明することはできますか? – ABCD

+0

こんにちは、それは本当にソケットとは何の関係もありません。あなたが使用しているURLが有効なURLではないということです。これはURLオブジェクトのインスタンス化の標準検証の一部です。 – david99world

+0

私はURLをhttp:// localhost:8080 /に設定しましたが、同じ例外があります。 主な問題は「windowsagentadd」の意味を知らないことです! – ABCD

関連する問題