当我发送GET请求时,为什么会出现错误500?
我试图发送一个简单的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());
}
}
错误500表示在服务器端错误。这通常是由服务器上的脚本生成无效响应标头(通常由脚本生成异常导致的)造成的。您的IOException是因为您的代码未设置为处理这些错误代码。
该错误意味着'内部服务器错误'。我认为这可能是由于对SSL主机进行常规请求造成的(请参阅url中的httpS前缀)。您可能未发送有效的HTTPS请求,并且服务器通过引发未处理的错误来处理此错误。
标准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();
仍然无法正常工作,但错误已经改变了一点: '线程异常“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
查看您的服务器应用程序lication日志... – EricParis16 2011-05-22 19:15:05
这不幸是不是我的服务器...嗯,我想我会放弃这个项目 – ldavin 2011-05-22 19:24:42
你能后你写的代码? – isNaN1247 2011-05-22 15:48:38
在daniweb上发布了相同的东西。由于同样的原因,没有得到回复,错误的描述,缺少代码 – 2011-05-22 15:50:50