2016-07-12 80 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對象的實例化的標準驗證的一部分。 – david99world

+0

我設定的網址爲http://本地主機:8080 /,但我得到了同樣的異常, 的主要問題是我不知道的「windowsagentadd」的含義! – ABCD

相關問題