Writing Plugins For Windows Live Writer – Using PluginHttpRequest Instead of HttpWebRequest

In a few of my plugins, I often find myself using System.Net.HttpWebRequest to get some data from a website, whether from a service or to access a specific part of a website. This is all very well and good, but what happens if you’re trying to use Windows Live Writer behind a proxy? Well with Writer itself, that’s not a problem, you just use the proxy settings to bypass it:
image_thumb Developer

The only trouble is, although Writer is using those proxy settings, your plugin isn’t. Unless you do more work to set the proxy in the WebRequest, but that means hard coding the details into your plugin, which won’t really work, or creating a seperate set of settings for it, which again is a but cumbersome. Fortunately though, the Writer team have given us an answer for this. Embedded deep in the Writer APIs is a little class called PluginHttpRequest. What this class does is use the settings that are already configured in Writer. How cool is that!

Now, one important thing to note about this class: it is not exactly the same as HttpWebRequest, more like a very good substitute. If you have built a plugin that uses HttpWebRequest, you will still have to modify some of the code. To demonstrate this, I created a test plugin that will show the HttpWebRequest method, and the PluginHttpRequest method.

I created the basics of a quick plugin, and used the following UI:

image_5 Developer

The code behind the Test 1 button looks like this:

        private void button1_Click(object sender, EventArgs e)
        {
            string s = "";
            try
            {
                // Create the HttpWebRequest
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                WebResponse resp = req.GetResponse();
                Stream st = resp.GetResponseStream();
                StreamReader sr = new StreamReader(st);
                s = sr.ReadToEnd();
            }
            catch (Exception ex)
            {
                s = "Failed to get information: n" + ex.Message;
            }
            textBox1.Text = s;
        }

The code behind the Test 2 button looks like this:

        private void button2_Click(object sender, EventArgs e)
        {
            string p = "";
            try
            {
                PluginHttpRequest prequest = new PluginHttpRequest(url);
                Stream presp = prequest.GetResponse();
                StreamReader sr = new StreamReader(presp);
                p = sr.ReadToEnd();
            }
            catch (Exception ex)
            {
                p = "Failed to get information: n" + ex.Message;
            }
            textBox2.Text = p;
        }

As you can see, there is one step that is missed out when using the PluginHttpRequest, and that’s getting the WebResponse and getting the stream from that. This is where the PluginHttpRequest differs, in that the GetResponse() returns a Stream, not a WebResponse.

The results of these tests are as follows:

image_7 Developer

This is for me at home, not behind a proxy, see how they both work, now if I was behind a proxy, Test 1 will give a failure message in the textbox, but Test 2 (assuming you have Writer configured for your proxy), will show the same as it does above, the source code for www.microsoft.com.

Now, this is only a basic example of how to use the PluginHttpRequest, and there are other properties to this class that can be levied. But if your plugin uses the methods described in Test 1, I would recommend migrating over to Writer’s native class, as it means your plugins will not only work for Joe Public, but also for those who are behind a corporate proxy, for example.

Test project source code: http://cid-fabdddc5cad93494.skydrive.live.com/self.aspx/LiveSide%20-%20Public/SourceCode/HttpRequestTest.zip

SL