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

标签 java rpc ping specifications

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

我看过这个,它似乎是 pingback 的(官方?)规范。
http://www.hixie.ch/specs/pingback/pingback-1.0

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

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

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

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

这是怎么回事?我意识到 pingback 是 web 组合在一起的东西,它成为了一个标准——但我对编码的内容感到困惑,或者确实相信响应。我可以相信下面的吗?它适用于所有博客 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()) ;
}

最佳答案

Can I trust the below? and will it work for all blog ping servers?

简短的回答是否定的。不同的服务器实现会存在错误或误解规范,因此您无法编写适用于所有博客 ping 服务器的代码。你能做的最好的事情就是对你接受的东西保持自由,并尽可能地尝试处理非标准/错误的服务器。

pingback spec说,

If the pingback request is successful, then the return value MUST be a single string, containing as much information as the server deems useful. This string is only expected to be used for debugging purposes.

If the result is unsuccessful, then the server MUST respond with an RPC fault value. The fault code should be either one of the codes listed above, or the generic fault code zero if the server cannot determine the correct fault code.

因此,希望服务器符合规范的客户端会做类似的事情,

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

关于java - 什么时候规范不是规范 - 全能的 RPC 博客 ping 规范难题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11557279/

相关文章:

go - 带有 AMQP 的 Golang 中的 JSON RPC

java - 工作流设计模式与任务模式相结合?

java - Eclipse: 导入 "Existing Maven Projects"自动创建 EAR 项目

java - Eclipse IDE 边栏(导航)缺失

德尔福印地 Ping 错误 10040

winapi - IcmpSendEcho2 因 WSA_QOS_ADMISSION_FAILURE 和 ERROR_NOACCESS 失败而失败

java - 如何获取发送和接收 ping 请求的时间间隔

java - 在 REST 客户端中反序列化 JsonObject

java - 生成动态文件并在 GWT 中下载

c - RPC 函数返回结构;客户值(value)观垃圾