我有一个使用 CookComputing.XmlRpc 库的 MS Visual C# 2010 XML-RPC 客户端应用程序。当它收到格式良好的输入时,它工作得很好。以下是基本的代码元素:
using CookComputing.XmlRpc;
...
public interface IXml : IXmlRpcProxy
{
[XmlRpcMethod]
MyResponse SendRequest(MyRequest request);
}
...
// create instance of XML-RPC interface
IXml proxy = XmlRpcProxyGen.Create<IXml>();
// set URL to selected end point
proxy.Url = MyUrl;
// set the methodName
proxy.XmlRpcMethod = XmlRpcMethod;
// set request variables
MyRequest request;
string data = txtSend.Text;
request.Request = data;
// talk to server and get response
try
{
MyResponse ret = proxy.SendRequest(request);
...
}
...
catch (Exception ex)
{
txtReceive.ForeColor = Color.Red;
txtReceive.Text = ex.ToString();
}
到目前为止,一切都很好。但是,有时我会从服务器收到无效 XML-RPC 的响应。(我无法控制。)例如:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head>
<title>404 Not Found</title>
</head>
<body>
<h1>Not Found</h1>
<p>The requested URL /ngt_jrn_stg was not found on this server.</p>
</body>
</html>
发生这种情况时,CookComputing XML-RPC 库会返回错误:
CookComputing.XmlRpc.XmlRpcServerException: Not Found
at CookComputing.XmlRpc.XmlRpcClientProtocol.ReadResponse(XmlRpcRequest req, WebResponse webResp, Stream respStm, Type returnType)
at CookComputing.XmlRpc.XmlRpcClientProtocol.Invoke(Object clientObj, MethodInfo mi, Object[] parameters)
at XmlRpcProxyf2e1082a-4493-486e-9034-cea828aa54d4.SendRequest(MyRequest request)
at TestNgt.TestNgt.btnProcess_Click(Object sender, EventArgs e) in ...\Main.cs:line 328
现在(如您在上面的代码中所见),我捕获了错误并显示了异常文本。我想知道是否有一种方法可以更优雅地处理此错误。理想情况下,我想显示类似“404 Not Found: The requested URL /ngt_jrn_stg was not found on this server.”的内容。(来自服务器响应 HTML)。
我捕获了来自 XML-RPC 调用的错误消息,但我找不到以编程方式访问导致错误的实际 HTML 响应的方法。(我通过在我用来与服务器通信的端口上设置一个 TcpTrace 实例找到了上面显示的文本。)
链接到 XML-RPC.NET 文档:http: //xml-rpc.net/faq/xmlrpcnetfaq-2-5-0.html