1

I am trying to send a simple GET request, as it is explained here : Using java.net.URLConnection to fire and handle HTTP requests

The web page I'm pointing to is : https://e-campus.hei.fr/ERP-prod/

And I get this HTTP500 error :

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)

Why do I receive this error for this page ? The code i wrote will return me the source code of any other web page...

My code :

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

}

}

Community
  • 1
  • 1
ldavin
  • 427
  • 2
  • 5
  • 16

3 Answers3

2

The standard java.net.URL class doesn't support the HTTPS protocol. You must set a system property and add a new security provider to the Security class object. There are a variety of ways to do both of these things, but try this :

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

append the port number if different from 443

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

   URLConnection con = URL.openConnection();
   //SSLException thrown here if server certificate is invalid
   con.getInputStream();

you have to catch SSLException if certificate is not trusted...

final code look like this :

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();
EricParis16
  • 759
  • 4
  • 8
  • Still not working, but the error has changed a bit : `Exception in thread "main" java.io.IOException: Server returned HTTP response code: 500 for URL: https://e-campus.hei.fr/ERP-prod/pc_mv_login.aspx at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436) at com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl.getInputStream(HttpsURLConnectionOldImpl.java:204) at GetWebPage.main(GetWebPage.java:33)` I really don't understand why, because it was working (and still does after your modification) with https://encrypted.google.com/ ... – ldavin May 22 '11 at 18:05
  • It is unfortunately not my server... Well I guess i'll drop this project – ldavin May 22 '11 at 19:24
1

Error 500 indicates an error on the server side. This is often caused by a script on the server generating invalid response headers (often, in turn, caused by the script generating an exception). Your IOException is because your code isn't set up to handle these error codes.

Ted Hopp
  • 222,293
  • 47
  • 371
  • 489
1

That error means 'Internal server error'. I think it might be caused by doing a regular request to an SSL host (See the httpS prefix in the url). You are probably not sending a valid HTTPS request, and the server handles this incorrectly by raising an unhandled error.

GolezTrol
  • 109,399
  • 12
  • 170
  • 196