我已经开始使用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 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()) ;
}
最佳答案
我可以相信以下内容吗?并适用于所有博客ping服务器吗?
简短的回答是否定的。不同的服务器实现会有错误或会误解规范,因此您无法编写适用于所有博客ping服务器的代码。您能做的最好的就是放宽您接受的内容,并尽力处理非标准/越野车服务器。
pingback spec说,
如果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) ...