2016-07-12 50 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

相关问题