|
A simple xml-rpc endpoint in .NET
It's easy to bypass the entire ASP.NET infrastructure on a .NET framework/IIS system to receive and respond to HTTP post requests. All you have to do is create a C# class library that implements the (single-method) IHttpHandler interface, put the executable in the right place, and reference it in a web.config file.
Here's the simple class I wrote (or more accurately, modified from the Microsoft sample) to log rss.xml file change notifications. (You can register for these notifications by sending a separate xml-rpc message to Userland's xmlStorageSystem, as described on this page). using System; using System.Web; using System.IO; namespace HandlerExample { public class MyHttpHandler : IHttpHandler { private void logMessage(string header,string message) { FileStream fs = new FileStream("c:\\logs\\log.txt",FileMode.Append,FileAccess.Write); StreamWriter w = new StreamWriter(fs); w.WriteLine("--- " + header); w.WriteLine(message); w.Flush(); w.Close(); } // Override the ProcessRequest method. public void ProcessRequest(HttpContext context) { StreamReader r = new StreamReader(context.Request.InputStream); logMessage("xml-rpc request",r.ReadToEnd()); r.Close(); String successResponse = "<?xml version=\"1.0"?>n"; successResponse += "<methodResponse>\n"; successResponse += "<params>\n"; successResponse += "<param>\n"; successResponse += "<value><boolean>1</boolean></value>\n"; successResponse += "</param>\n"; successResponse += "</params>\n"; successResponse += "</methodResponse>\n"; context.Response.ContentType = "text/xml"; context.Response.Write(successResponse); } // Override the IsReusable property. public bool IsReusable { get { return true; } } } }
The lone method in the interface is ProcessRequest. Its one parameter, an HttpContext object, is stocked with everything you need to read and respond to the HTTP request. I use the Request property of the HttpContext to get the "post" data, and the Response property to return my xml-rpc response message.
To build this class, just run the command: csc /t:library /r:System.Web.dll HandlerTest.cs
This will create a file called HandlerTest.dll in the same directory.
To hook it up, just copy the dll into inetpubwwwrootbin (you might have to create it; I did). Then create another subdirectory under inetpubwwwroot (I called mine HandlerDir) and create a file in it called web.config. Here's what it should contain:
<configuration> <system.web> <httpHandlers> <add verb="*" path="handler.aspx" type="HandlerExample.MyHttpHandler,HandlerTest"/> </httpHandlers> </system.web> </configuration>
Once this is set up, any HTTP requests to /HandlerDir/handler.aspx will go to your class instead of to ASP.NET. You can test this in a browser. I created this HTML file in the HandlerDir directory (I called it test.html): <html> <body> <form action="handler.aspx" method="POST"> <input name="data" type="text"> <input type="submit"> </form> </body> </html>
When I browse to it and click "Submit", I see the xml-rpc response in the browser and get the "data=" message in my log file.
That's all there is to it. Set it up and your .NET system talks (a very, very basic) xml-rpc.
Coming soon: an article showing the code I used to register my callback with xmlStorageSystem.
|
© Copyright 2003 Jim Klopfenstein.
Last update: 2/10/2003; 8:43:42 AM.
|
|