Jsoup 到 post 数据并解析 CFscript 上的替代 URL

Jsoup to post data and parse alternative URLs on CFscript

我需要使用 CFscript 中的 Jsoup 从 primary_URL 获取并解析页面。

如果页面状态不正常或数据已损坏或为空,我应该尝试 secondary_URL 中的替代页面。

primary_URL 只接受 POST 请求,我不知道如何在 cfscript

中做到这一点

secondary_URL 默认接受 GET

这是一个想法:

<cfscript>
jsoup = createObject("java", "org.jsoup.Jsoup");
response = jsoup.connect(primary_URL).userAgent("#CGI.Http_User_Agent#").timeout(10000).method(Connection.Method.POST).execute();  // How to use Method.POST in this case???
if(response.statusCode() == 200)
{
    doc = response.parse();
    theData = doc.select("div##data");
    ...
    `some other parsing and SQL UPDATE routine`
}
else
{
    response = jsoup.connect(secondary_URL).userAgent("#CGI.Http_User_Agent#").timeout(10000).execute();  // default is GET
    if(response.statusCode() == 200)
    {
        doc = response.parse();
        theData = doc.select("div##same_data");
        ...
        `some other parsing and SQL UPDATE routine`
    }
}
</cfscript>

如果响应正常但数据似乎损坏或为空,如何跳转到 secondary_URL?一种 goto 运算符?

运行 ColdFusion 11.

How to jump to the secondary_URL in case the response is OK but the data appears to be currupt or empty? A kind of goto operator?

不是只检查 statusCode,而是调用一个函数。在此函数内执行所有必要的检查(损坏的数据、空数据......)。

<cfscript>

    function IsValid(response) {
       // Perform all the tests here...
       // Return TRUE on success or FALSE otherwise

       return true;
    }

    jsoup = createObject("java", "org.jsoup.Jsoup");
    response = jsoup //
                 .connect(primary_URL) //
                 .userAgent("#CGI.Http_User_Agent#") //
                 .timeout(10000) //
                 .post();  // Simply call the post() method for posting...
    if( IsValid(response) ) {

    } else {
        response = jsoup //
                    .connect(secondary_URL) //
                    .userAgent("#CGI.Http_User_Agent#") //
                    .timeout(10000) //
                    .get();  // Make your intent clear

        if ( IsValid(response) ) {
            // ...
        }
    }

</cfscript>