我有一个使用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
现在(正如您在上面的代码中看到的),我捕获错误并显示异常文本。我想知道的是,如果有一种方法可以更优雅地处理这个错误。理想情况下,我想显示类似于&#34; 404 Not Found:在此服务器上找不到请求的URL / ngt_jrn_stg。&#34; (来自服务器响应HTML)。
我从XML-RPC调用中捕获错误消息,但我找不到以编程方式访问导致错误的实际HTML响应的方法。 (我通过在用于与服务器通信的端口上设置TcpTrace实例找到了上面显示的文本。)
链接到XML-RPC.NET文档:http://xml-rpc.net/faq/xmlrpcnetfaq-2-5-0.html