2012-03-26 60 views
0

我不斷收到這個錯誤,我真的不知道爲什麼。我嘗試過不同的網址,但我仍然遇到同樣的錯誤。java.net.MalformedURLException java.net.URL。 <init>(未知來源)

java.net.MalformedURLException 的java.net.URL。(來源不明)

public class ProxyServlet extends HttpServlet { 

private String PostUrl = "http://localhost:8080/myProxy/myServlet"; 

private static final long serialVersionUID = 1L; 

public ProxyServlet() { 
    super(); 
    } 

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

    System.out.println("Parameters retreived..."); 
    // map parameters to properties in config file and set response 
    Map(); 
    } 

private void Map() throws IOException{ 


     String urlParameters = "topic="+topic+"&item="+item+"&period="+period+"; 
     URL url = new URL(PostUrl); 
     HttpURLConnection conn = (HttpURLConnection)url.openConnection(); 
     System.out.println("Connection made to " + PostUrl); 
     conn.setDoOutput(true); 
     conn.setInstanceFollowRedirects(true); 
     conn.setRequestMethod("POST"); 
     conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
     conn.setRequestProperty("charset", "utf-8"); 

     OutputStream out = null; 

     try{ 
      System.out.println("Writing data to output stream..."); 
      out = conn.getOutputStream(); 
      out.write(urlParameters.getBytes()); 
      out.close(); 
      System.out.println("All done!!!"); 
     } 

     catch(IOException e){ 
      e.printStackTrace(); 
     } 
    } 
} 

任何幫助,將不勝感激!

謝謝!

回答

1

此代碼不能編譯。

您的網址無效。由於上述原因,不能認爲它如上所示。

此外,您還必須URLEncode參數名稱和值。

+0

謝謝。我有URLEncoded參數。你爲什麼認爲該網址無效?我很確定URL存在。即使我嘗試[http://yahoo.com](http://yahoo.com)之類的東西,我仍會得到相同的錯誤。 – user1192724 2012-03-26 02:38:33

+0

@ user1192724這是無效的,因爲例外如此。格格不入,不只是不存在。仔細檢查。 – EJP 2012-03-26 02:43:40

+0

謝謝!我看到我正在修改我的代碼中其他地方的url變量,並將其設置爲null。修正了這一點,它完美的作品。謝謝! – user1192724 2012-03-26 03:08:53

相關問題