2011-05-22 59 views
1

我試圖發送一個簡單的GET請求,因爲它是在這裏解釋:Using java.net.URLConnection to fire and handle HTTP requests當我發送GET請求時,爲什麼會出現錯誤500?

我指向的網頁是:https://e-campus.hei.fr/ERP-prod/

而且我得到這個錯誤HTTP500:

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 500 for URL: https://e-campus.hei.fr/ERP-prod/ 
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436) 
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234) 
at GetWebPage.main(GetWebPage.java:14) 

爲什麼我收到此頁錯誤?我寫將返回我任何其他網頁的源代碼,該代碼...

我的代碼:

public class GetWebPage { 
public static void main(String args[]) throws MalformedURLException, 
     IOException { 
    URLConnection connection = new URL("https://e-campus.hei.fr/ERP-prod/").openConnection(); 
    connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401"); 
    InputStream response = connection.getInputStream(); 

    InputStreamReader isr = new InputStreamReader(response); 
    BufferedReader reader = new BufferedReader(isr); 
    StringBuilder sb = new StringBuilder(); 
    String line = ""; 
    while ((line = reader.readLine()) != null) { 
     sb.append(line + "\n"); 
    } 
    System.out.println(sb.toString()); 

} 

}

+1

你能後你寫的代碼? – isNaN1247 2011-05-22 15:48:38

+0

在daniweb上發佈了相同的東西。由於同樣的原因,沒有得到回覆,錯誤的描述,缺少代碼 – 2011-05-22 15:50:50

回答

1

錯誤500表示在服務器端錯誤。這通常是由服務器上的腳本生成無效響應標頭(通常由腳本生成異常導致的)造成的。您的IOException是因爲您的代碼未設置爲處理這些錯誤代碼。

1

該錯誤意味着'內部服務器錯誤'。我認爲這可能是由於對SSL主機進行常規請求造成的(請參閱url中的httpS前綴)。您可能未發送有效的HTTPS請求,並且服務器通過引發未處理的錯誤來處理此錯誤。

2

標準java.net.URL類不支持HTTPS協議。 您必須設置系統屬性並將新的安全提供程序添加到Security類對象。有多種方法可以做到這兩個東西,但試試這個:

System.setProperty("java.protocol.handler.pkgs", 
     "com.sun.net.ssl.internal.www.protocol"); 
    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); 

追加端口號,如果從443

URL url = new URL("https://[your server]:7002"); 

    URLConnection con = URL.openConnection(); 
    //SSLException thrown here if server certificate is invalid 
    con.getInputStream(); 
你必須趕上異常SSLException如果證書不被信任

不同。 ..

最終代碼如下所示:

System.setProperty("java.protocol.handler.pkgs", 
     "com.sun.net.ssl.internal.www.protocol"); 
    try 
    { 
    //if we have the JSSE provider available, 
    //and it has not already been 
    //set, add it as a new provide to the Security class. 
    Class clsFactory = Class.forName("com.sun.net.ssl.internal.ssl.Provider"); 
    if((null != clsFactory) && (null == Security.getProvider("SunJSSE"))) 
     Security.addProvider((Provider)clsFactory.newInstance()); 
    } 
    catch(ClassNotFoundException cfe) 
    { 
     throw new Exception("Unable to load the JSSE SSL stream handler." + 
     "Check classpath." + cfe.toString()); 
    } 


    URL url = new URL("https://[your server]:7002"); 
    URLConnection con = URL.openConnection(); 
    //SSLException thrown here if server certificate is invalid 
    con.getInputStream(); 
+0

仍然無法正常工作,但錯誤已經改變了一點: '線程異常「main」java.io.IOException:服務器返回的HTTP響應代碼:500 for URL:https://e-campus.hei.fr/ERP-prod/pc_mv_login.aspx \t at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436) \t at com.sun .net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl.getInputStream(HttpsURLConnectionOldImpl.java:204) \t at GetWebPage.main(GetWebPage.java:33)' 我真的不明白爲什麼,因爲它在工作(並且在您修改後仍然會)https://encrypted.google.com/ ... – ldavin 2011-05-22 18:05:30

+0

查看您的服務器應用程序lication日誌... – EricParis16 2011-05-22 19:15:05

+0

這不幸是不是我的服務器...嗯,我想我會放棄這個項目 – ldavin 2011-05-22 19:24:42

相關問題