java.io.IOException:服务器返回HTTP响应代码:500表示URL

时间:2012-07-12 13:33:00

标签: java xml-parsing jdom

我有以下代码来调用网址:http://10.105.0.120/24online/servlet/ClientRegistrationResponse?action=response&phone=919638983856&actioncode=0000

代码:

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

但是我收到了以下错误:

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*****************

出现此错误的原因是什么?就像我直接从浏览器调用URL一样,它的工作非常完美。

1 个答案:

答案 0 :(得分:1)

有问题吗?

您向服务器发送了错误请求,并且它正在响应内部服务器错误(代码500)。合乎逻辑的做法是检查您的HTTP服务器日志(它是10. * IP地址,因此它是您本地的。)

此外,JDOM异常位于不同的线程中......您显示的代码中没有与JDOM相关的代码,并且异常堆栈跟踪没有以任何方式链接到具有500状态响应的IOException。或者,您忽略了状态500响应(IOException),然后期望同一线程中的JDOM代码解析来自服务器的(可能是空的)错误响应(毫无疑问会在过早结束时结束)文件)。

你需要:

  • 检查您的服务器日志
  • 妥善处理所有例外
  • 可能会添加一些调试代码,以便您检查发送到服务器/从服务器检索的数据。