2012-07-19 82 views
4

我已經開始使用Apache RPC客戶端庫在Java中實現博客ping服務。然而,我有點困惑,我似乎無法找到博客ping響應應該看起來像檢查它是否成功的明確規範。何時規範不是規範 - 全能RPC博客ping規範難題

我看過這個,它似乎是pingback的(官方?)規範。
http://www.hixie.ch/specs/pingback/pingback-1.0

然而,這提到將返回故障碼,例如,

http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php

許多RPC服務器,如谷歌博客搜索,似乎回到了「flerror」,並在其XML響應「消息」元素,這似乎更像是這樣的:

http://xmlrpc.scripting.com/weblogsCom.html

這是怎麼回事?我意識到pingback是網絡類型一起入侵的東西,並且它成爲了一個標準 - 但我很困惑要編碼什麼,或者確實相信響應。我可以信任以下嗎?並且它將適用於所有博客ping服務器?

public boolean ping(String urlToPing, String title, String url, String urlChanges, String urlRSS) throws MalformedURLException, XmlRpcException 
{ 
    XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); 
    config.setServerURL(new URL(urlToPing)); 

    XmlRpcClient client = new XmlRpcClient(); 
    client.setConfig(config); 

    Object[] params = new Object[] { title, url, urlChanges, urlRSS }; 
    HashMap result = (HashMap)client.execute("weblogUpdates.extendedPing", params); 

    try 
    { 
     errors.put(url, Boolean.parseBoolean(result.get("flerror").toString())); 
    } 
    catch(Exception e) 
    { 
     log.error("RPC Problem Parsing response to Boolean trying: " + result.get("flerror")); 
    } 

    return Boolean.parseBoolean(result.get("flerror").toString()) ; 
} 

回答

2

我可以信任以下嗎?並且它將適用於所有博客ping服務器?

最簡答案是否定的。不同的服務器實現會產生錯誤或誤解規範,因此您無法編寫適用於所有博客ping服務器的代碼。你所能做的最好的就是在你接受的內容中保持自由,並且儘可能地處理非標準/錯誤的服務器。

pingback spec說,

如果參照通知請求是成功的,則返回值必須是一個單個 串,含有作爲服務器認爲 有用盡可能多的信息。該字符串只能用於調試 的目的。

如果結果不成功,那麼服務器必須響應一個 RPC故障值。故障代碼應該是上面列出的代碼 之一,或者如果服務器不能確定正確的故障代碼,則通用故障代碼爲零。

所以客戶希望服務器遵守該規範會做這樣的事情,

try { 
    client.execute("weblogUpdates.extendedPing", params); 
} catch(XmlRpcException e) { 
    //check the code of the rpc exception as shown below, 
    //log the error, or perhaps rethrow it? 
    return false; 
} 

如果服務器是繼該通告規範,它應該返回下列錯誤代碼之一,

0 
A generic fault code. Servers MAY use this error code instead of any of the others if they do not have a way of determining the correct fault code. 
0×0010 (16) 
The source URI does not exist. 
0×0011 (17) 
The source URI does not contain a link to the target URI, and so cannot be used as a source. 
0×0020 (32) 
The specified target URI does not exist. This MUST only be used when the target definitely does not exist, rather than when the target may exist but is not recognised. See the next error. 
0×0021 (33) 
The specified target URI cannot be used as a target. It either doesn't exist, or it is not a pingback-enabled resource. For example, on a blog, typically only permalinks are pingback-enabled, and trying to pingback the home page, or a set of posts, will fail with this error. 
0×0030 (48) 
The pingback has already been registered. 
0×0031 (49) 
Access denied. 
0×0032 (50) 

至於你提到的幾個參照通知服務器返回錯誤代碼,讓你有這樣的代碼以檢查爲好,

try { 
    Object rpcRVal = client.execute("weblogUpdates.extendedPing", params); 
    if(rpcRVal instanceof Map) { 
     Object flError = ((Map) rpcRVal).get("flerror"); 
     if(flError != null && flError instanceof Boolean) { 
      return ((Boolean) flError).booleanValue());   
     } 
    } 
    return true; 
} catch(XmlRpcException e) ...