Java 如何使用SaxParser.parse捕获(http)响应代码

Java 如何使用SaxParser.parse捕获(http)响应代码,java,xml-parsing,Java,Xml Parsing,我使用SaxParser解析从(OAI)服务器接收的xml。这个xml可以从我向服务器生成另一个请求,以获取另一个xml,依此类推 一些服务器可以将这些后续请求理解为拒绝服务攻击,并返回“HTTP响应代码:503” 如何捕获响应代码 以下是我正在使用的代码: try { InputSource is = new InputSource( uri ); is.setEncoding( "UTF-8" ); parser.parse( is,

我使用SaxParser解析从(OAI)服务器接收的xml。这个xml可以从我向服务器生成另一个请求,以获取另一个xml,依此类推

一些服务器可以将这些后续请求理解为拒绝服务攻击,并返回“HTTP响应代码:503”

如何捕获响应代码

以下是我正在使用的代码:

try
    {
        InputSource is = new InputSource( uri );
        is.setEncoding( "UTF-8" );
        parser.parse( is, handler );
        parsed = true;
    }
    catch ( IOException ioException )
    {
        ...here should happen the response code handling...

        persistError( "Error(IOException) in parsing: " + ioException.getMessage() );
    }
    catch ( SAXException saxException )
    {
        persistError( "Error(SAXException) in parsing: " + saxException.getMessage() );
        // we do not retry to parse if the error is a parsing error
        retry = RETRY_FETCH_URL + 1;
    }

在调用解析器之前,始终可以检查响应代码。例如:

HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
final int responseCode = connection.getResponseCode();
if (responseCode < 200 || responseCode > 299) {
    log.info("Response " + responseCode + " opening connection to " + url);
    return;
}
try (Reader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
    InputSource is = new InputSource(reader);
    is.setEncoding( "UTF-8" );
    parser.parse(is, handler);
}
HttpURLConnection connection=(HttpURLConnection)新URL(URL).openConnection();
final int responseCode=connection.getResponseCode();
如果(响应代码<200 | |响应代码>299){
log.info(“响应”+responseCode+”打开到“+url”的连接);
返回;
}
try(Reader Reader=new BufferedReader(new InputStreamReader(connection.getInputStream(),“UTF-8”)){
InputSource is=新的InputSource(读卡器);
is.setEncoding(“UTF-8”);
parser.parse(is,handler);
}

在调用解析器之前,您始终可以检查响应代码。例如:

HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
final int responseCode = connection.getResponseCode();
if (responseCode < 200 || responseCode > 299) {
    log.info("Response " + responseCode + " opening connection to " + url);
    return;
}
try (Reader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
    InputSource is = new InputSource(reader);
    is.setEncoding( "UTF-8" );
    parser.parse(is, handler);
}
HttpURLConnection connection=(HttpURLConnection)新URL(URL).openConnection();
final int responseCode=connection.getResponseCode();
如果(响应代码<200 | |响应代码>299){
log.info(“响应”+responseCode+”打开到“+url”的连接);
返回;
}
try(Reader Reader=new BufferedReader(new InputStreamReader(connection.getInputStream(),“UTF-8”)){
InputSource is=新的InputSource(读卡器);
is.setEncoding(“UTF-8”);
parser.parse(is,handler);
}

解析器是您最不希望“捕获”web异常的地方,除非异常以XML形式返回并且您希望将其解析出来。您是否尝试在HTTP请求级别捕获异常?逻辑通常沿着if HTTP响应!=200-->做点什么来处理这个问题。否则-->解析响应。您应该能够通过查看HTTP请求返回的头来检查HTTP响应代码,而不必解析它。解析器是您最不希望“捕获”web异常的位置,除非异常以XML形式返回并且您希望解析它。您是否尝试在HTTP请求级别捕获异常?逻辑通常沿着if HTTP响应!=200-->做点什么来处理这个问题。否则-->解析响应。您应该能够通过查看HTTP请求返回的头来检查HTTP响应代码,而不必解析它。