java 为什么我在发送 GET 请求时会收到错误 500?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6089018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:16:06  来源:igfitidea点击:

Why do I get an error 500 when I send a GET request?

java

提问by ldavin

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

我正在尝试发送一个简单的 GET 请求,如下所述: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/

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

And I get this HTTP500 error :

我收到这个 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)

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

}

}

}

回答by EricParis16

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 :

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

append the port number if different from 443

如果与 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...

如果证书不受信任,您必须捕获 SSLException...

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

回答by Ted Hopp

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.

错误 500 表示服务器端出现错误。这通常是由服务器上的脚本生成无效响应标头引起的(通常反过来,由脚本生成异常引起)。您的 IOException 是因为您的代码未设置为处理这些错误代码。

回答by GolezTrol

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.

该错误意味着“内部服务器错误”。我认为这可能是由向 SSL 主机发出常规请求引起的(请参阅 url 中的 httpS 前缀)。您可能没有发送有效的 HTTPS 请求,并且服务器通过引发未处理的错误来错误地处理此请求。