检查HTTP 500错误HttpUrlConnection Java的缺陷

时间:2013-06-20 14:43:04

标签: java http httpclient httpurlconnection httpexception

我的问题在于status != 200,当我在不同时间运行此脚本(3)时,它会打印出if{}(1)次的值,else{}(1 )时间和catch{}另一个。

我只是想在if(status != 200 || showTitleAttr == null || showTitleAttr == "") system.out.println();中打印出来。如果HTTP错误不是HTTP 200错误代码,则显示消息。 逻辑是有道理的,但它仍然不能一直工作并落入catch{}块。我开始认为这是变量status的问题。

谢谢。

小更新 即使我尝试:conn.getResponseCode() != HttpURLConnection.HTTP_OK它似乎也没有改变它落入的块。(1)if{}else{}catch{}

的时间

我遇到catch{}块时收到的错误是:

java.io.IOException: Server returned HTTP response code: 500 for URL

问:有没有更好的方法来检查HTTP错误代码,而不是HTTP 200错误?

代码:

 public static void main(String args[]) {
        HttpException HttpExc = new HttpException();
        try {
                // setup Show Title Parser
                DOMParser parser = new DOMParser();

                // Set the Url to Parse and assign to object
                String UrlToParse = "urlishere.com";
                    URL obj = new URL(UrlToParse);

                // Try opening a http connection the the url above
                HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
                int status = conn.getResponseCode();    

                // Setup parser
                parser.parse(UrlToParse);  
                Document doc = parser.getDocument();

                // Get the document's root XML node
                NodeList PGRoot = doc.getChildNodes();

                // Navigate down the hierarchy to get to the program node
                Node comp = getNode("ProgramGuideWCSResponse", PGRoot);
                Node PG = getNode("programGuide", comp.getChildNodes() );
                Node Programs = getNode("programs", PG.getChildNodes() );
                Node IndivdualProgram = getNode("program", Programs.getChildNodes() );
                NodeList nodes = IndivdualProgram.getChildNodes();
                //String execType = getNodeAttr("programTitle", exec);

                // Assign the Show Title to the NodeValue from traversing
                String showTitleAttr = getNodeValue("programTitle", nodes);

                // check to if the fetched Show Title isn't: 1) Returned bad error code 2) is null or 3) is empty 
                    // if it is... display the default value for title and exit.
                    // otherwise, print the Program Title.

                if(status != 200 || showTitleAttr == null || showTitleAttr == ""){
                    System.out.println("You’re watching XX Plus");
                    System.exit(0);
                }

                else{
                    System.out.println("Program title is: " + showTitleAttr);
                }

        }
            catch(HttpException e){
                e.getStackTrace();
            }

            catch (Exception e) {
                //e.printStackTrace();
                System.out.println("You’re watching XX Plus");
                System.exit(0);
            }
     }
}

1 个答案:

答案 0 :(得分:2)

您需要在开始解析之前检查状态...

    URL url = new URL(UrlToParse );
    HttpURLConnection con = (HttpURLConnection)url.openConnection();
    int status = con.getResponseCode();
    if (status == 200){
        parser.parse(UrlToParse);  
        ....
    } else {
        // status is not 200
    }