编程语言
首页 > 编程语言> > java – 什么时候规范不是规范 – 全能的RPC博客ping规范难题

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

作者:互联网

我已经开始使用Apache RPC Client库在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 Blog搜索)似乎在其XML响应中返回“flerror”和“message”元素,这似乎更类似于:

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()) ;
}

解决方法:

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,specifications,ping,rpc
来源: https://codeday.me/bug/20190704/1375040.html