2012-07-19 104 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) ...