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:

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:

The code behind the Test 1 button looks like this:

        private void button1_Click(object sender, EventArgs e)
            string s = "";
                // 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 = "";
                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:

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


PS, Expect a post soon with a list of my plugins that use the original HttpWebRequest class as they will have been updated and re-released.

  1. #1 by Jusweny on November 7, 2007 - 12:15 pm

    Wha\’t the correct usage? I use this utility, but test1 returns invalid proxy, test2 returns valid html. so, what the problem?

  2. #2 by Scott on November 7, 2007 - 12:29 pm

    I\’m guessing you\’re behind a proxy then 😉 You\’ve just proved exactly why plugins should use the PluginHttpRequest class. If you\’re behind a proxy, test 1 will fail, but test 2 should work (assuming you have the proxy set up in Writer).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: