java.io.IOException:服务器返回 HTTP 响应代码:URL 500
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11452967/
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
java.io.IOException: Server returned HTTP response code: 500 for URL
提问by chetan
I have following code to call URL : http://10.105.0.120/24online/servlet/ClientRegistrationResponse?action=response&phone=919638983856&actioncode=0000
我有以下代码来调用 URL : http://10.105.0.120/24online/servlet/ClientRegistrationResponse?action=response&phone=919638983856&actioncode=0000
Code :
代码 :
StringBuffer request=new StringBuffer();
request.append(xml);
URL url = new URL(url);
HttpURLConnection obj = null;
obj = (HttpURLConnection)url.openConnection(); //create a SSL connection object server-to-server
((URLConnection)obj).setDoInput(true);
((URLConnection)obj).setDoOutput(true);
((URLConnection)obj).setUseCaches(false);
// ((URLConnection)obj).setReadTimeout(10);
((URLConnection)obj).setRequestProperty("Content-Type","application/x-www-form-urlencoded");
obj.setRequestMethod("POST");
obj.setRequestProperty("charset", "US-ASCII");
//obj.setConnectTimeout(PropertyReader.IN_REQUEST_TIMEOUT);
//obj.setReadTimeout(PropertyReader.IN_REQUEST_TIMEOUT);
// Here the HTTPS request URL is created
DataOutputStream dataoutputstream = new DataOutputStream(((URLConnection)obj).getOutputStream());
dataoutputstream.writeBytes(request.toString()); //request
//dataoutputstream.flush();
dataoutputstream.close(); //connection closed
BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(((URLConnection)obj).getInputStream()));
String res = "";
while ( (res = bufferedreader.readLine()) != null ) {
conresponse += res;
}
bufferedreader.close();
But i am getting following error :
但我收到以下错误:
java.io.IOException: Server returned HTTP response code: 500 for URL: http://10.105.0.120/24online/servlet/ClientRegistrationResponse?action=response&phone=919638983856&actioncode=0000
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1313)
at cyberoam.corporate.integration.XMLSenderReceiver.sendFileData1(XMLSenderReceiver.java:70)
at cyberoam.corporate.modes.TopupRequestThread.sendTopupRequest(TopupRequestThread.java:56)
at cyberoam.corporate.modes.TopupRequestThread.run(TopupRequestThread.java:31)
org.jdom.input.JDOMParseException: Error on line -1: Premature end of file.
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:504)
at cyberoam.corporate.integration.XMLProcessor.parseXML(XMLProcessor.java:392)
at cyberoam.corporate.modes.TopupRequestThread.sendTopupRequest(TopupRequestThread.java:62)
at cyberoam.corporate.modes.TopupRequestThread.run(TopupRequestThread.java:31)
Caused by: org.xml.sax.SAXParseException: Premature end of file.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:489)
... 3 more
Caused by: org.xml.sax.SAXParseException: Premature end of file.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:489)
at cyberoam.corporate.integration.XMLProcessor.parseXML(XMLProcessor.java:392)
at cyberoam.corporate.modes.TopupRequestThread.sendTopupRequest(TopupRequestThread.java:62)
at cyberoam.corporate.modes.TopupRequestThread.run(TopupRequestThread.java:31)
Caused by: org.xml.sax.SAXParseException: Premature end of file.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:489)
at cyberoam.corporate.integration.XMLProcessor.parseXML(XMLProcessor.java:392)
at cyberoam.corporate.modes.TopupRequestThread.sendTopupRequest(TopupRequestThread.java:62)
at cyberoam.corporate.modes.TopupRequestThread.run(TopupRequestThread.java:31)
2012-07-12 18:52:49,416 - PIPE_COMM: In Receiving Object for /usr/local/nas/pipes/raam_to_gui_pipe
******************SMPPThread constructor called*****************
What is the reason for this error ? as when i directly call URL from browser it's work perfect.
这个错误的原因是什么?当我直接从浏览器调用 URL 时,它的工作完美无缺。
回答by rolfl
Is there a question?
有问题吗?
You have sent a bad request to the server, and it is responding with an internal server error (code 500). The logical thing to do is to check your HTTP server logs (it is a 10.* IP address, so it is local to you).
您向服务器发送了一个错误的请求,它正在响应内部服务器错误(代码 500)。合乎逻辑的做法是检查您的 HTTP 服务器日志(它是一个 10.* IP 地址,因此它对您来说是本地的)。
Also the JDOM Exceptions are in a different thread or something.... there is no JDOM related code in the code you show, and the exception stack traces are not linked in any way to the IOException with the 500 status response. Alternatively, you are ignoring the status-500 response (IOException), and then expecting the JDOM code in that same thread to parse a (probably empty) error-response from the server (which would no doubt end in a premature end-of-file).
此外,JDOM 异常在不同的线程或其他东西中......您显示的代码中没有与 JDOM 相关的代码,并且异常堆栈跟踪没有以任何方式链接到具有 500 状态响应的 IOException。或者,您忽略状态 500 响应 (IOException),然后期望同一线程中的 JDOM 代码解析来自服务器的(可能为空的)错误响应(这无疑会以过早结束-文件)。
You need to:
你需要:
- check your server logs
- handle all exceptions appropriately
- potentially add some debug code to enable you to inspect the data you send to / retrieve from the server.
- 检查您的服务器日志
- 适当地处理所有异常
- 可能会添加一些调试代码,使您能够检查发送到服务器/从服务器检索的数据。