2016-03-29 16 views
2

我需要在CFscript中使用Jsoup从primary_URL中获取和解析页面。Jsoup发布数据并解析CFscript上的备用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

回答

4

如何跳转到secondary_URL的情况下,响应是确定的,但该数据似乎currupt或空?一种goto运算符?

不是只检查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>