检查HTTP 500错误HttpUrlConnection Java时存在漏洞
我的问题是状态200,当我运行这个脚本(3)次时,它将打印出检查HTTP 500错误HttpUrlConnection Java时存在漏洞,java,http,httpclient,httpurlconnection,httpexception,Java,Http,Httpclient,Httpurlconnection,Httpexception,我的问题是状态200,当我运行这个脚本(3)次时,它将打印出if{}(1)次、else{}(1)次和catch{}另一次的值 我只是想在if(status!=200 | | | showTitleAttr==null | | | showTitleAttr==“”)中打印出来system.out.println();如果HTTP错误不是HTTP 200错误代码,则返回消息。 逻辑是有道理的,但它仍然没有一直工作,并落入catch{}块。我开始认为这是变量status的问题 多谢各位 小更新 即使
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
Q:有没有更好的方法来检查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);
}
}
}
您需要在开始分析之前检查状态
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
}
尝试将此
showTitleAttr==”
更改为此showTitleAttr.equals(“”
)。不确定这是否导致了您的问题,但感谢您的回复,尽管如上所述检测到Http错误,但这并不能解决问题。当它到达catch块时,异常堆栈跟踪是什么?请参阅上面的更新文章您正在调用parser.parse(UrlToParse),但没有先检查状态。如果状态为500(错误),parser.parse(UrlToParse)可能会引发异常。