Java 什么时候规范不是规范-万能的RPC博客ping规范难题

Java 什么时候规范不是规范-万能的RPC博客ping规范难题,java,rpc,ping,specifications,Java,Rpc,Ping,Specifications,我已经开始使用ApacheRPC客户端库在Java中实现博客ping服务。然而,我有点困惑,我似乎找不到一个明确的规范来说明博客ping响应应该是什么样子的,以检查它是否成功 我已经看过了,这似乎是pingback的(官方?)规范。 但是,这提到将返回故障代码,例如 许多RPC服务器(如Google博客搜索)似乎在其XML响应中返回“flerror”和“message”元素,这似乎更类似于: 这是怎么回事?我意识到pingback是一种被网络黑客攻击的东西,它成为了一种标准——但我不知道应

我已经开始使用ApacheRPC客户端库在Java中实现博客ping服务。然而,我有点困惑,我似乎找不到一个明确的规范来说明博客ping响应应该是什么样子的,以检查它是否成功

我已经看过了,这似乎是pingback的(官方?)规范。

但是,这提到将返回故障代码,例如

许多RPC服务器(如Google博客搜索)似乎在其XML响应中返回“flerror”和“message”元素,这似乎更类似于:

这是怎么回事?我意识到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()) ;
}
我能相信下面的人吗?它是否适用于所有博客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()) ;
}
简而言之,答案是否定的。不同的服务器实现会有bug或误解规范,因此您无法编写适用于所有博客ping服务器的代码。你能做的最好的事情就是在你所接受的内容上保持自由,并尽可能地处理非标准/有缺陷的服务器

他说,

如果pingback请求成功,则返回值必须为 单个字符串,包含服务器认为的尽可能多的信息 有用。此字符串仅用于调试 目的

如果结果不成功,则服务器必须使用 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;
} 
如果服务器遵循pingback规范,则应返回以下故障代码之一

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)
正如您所提到的,几个pingback服务器返回一个错误代码,因此您必须使用以下代码检查错误代码:

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) ...