Join LiveSide!
Sign In using: Name/Password OpenID
or Live ID: Sign In Live ID
0 out of 8,442 members
are online, & 61 guest(s).

Recent Comments

–ScottIsAFool
re: Writing Plugins For Windows Live Writer - Using PluginHttpRequest Instead of HttpWebRequest

–fauxpoet
re: Writing Plugins For Windows Live Writer - Using PluginHttpRequest Instead of HttpWebRequest

Log in or Join to leave a comment!

LiveSide on Mobile

Our latest posts and our favorite links,
all on your phone or mobile device
Visit m.LiveSide.net
Or go to www.LiveSide.net
on your mobile device and we'll redirect you!

Tweets We Like

Loading...

LiveSide Time

Redmond

Dallas (server)

London

Shanghai

Windows Live Calendar

Follow us on Facebook

    LiveSide.net
   
    Promote Your Page Too

  • LiveSide on the Windows Live Network
  • LiveSide on Facebook
  • LiveSide on Twitter
  • LiveSide RSS  
  • Windows Live Alerts
  •  
  • feedburner
  •  
LiveSide - Developer Blog

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

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

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

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

Comments

fauxpoet wrote re: Writing Plugins For Windows Live Writer - Using PluginHttpRequest Instead of HttpWebRequest
on Sun, Oct 21 2007 4:31 PM

I would love to see a guide on how to code Plugings for Writer.  That would be really cool :D

ScottIsAFool wrote re: Writing Plugins For Windows Live Writer - Using PluginHttpRequest Instead of HttpWebRequest
on Sun, Oct 21 2007 7:03 PM

fauxpoet, we already have such a guide ;) The first post in my series on doing this is here www.liveside.net/.../Writing-Plugins-For-Windows-Live-Writer-_2D00_-Getting-Started.aspx and if you check all the posts under the "How To" tag on here, you will easily find the rest.

SL

Writing Windows Live Writer Plugins - A Recap - LiveSide - Developer Blog wrote Writing Windows Live Writer Plugins - A Recap - LiveSide - Developer Blog
on Tue, Oct 23 2007 8:33 PM

Pingback from  Writing Windows Live Writer Plugins - A Recap - LiveSide - Developer Blog

?????? Windows Live Writer ?????? - ?????? - LiveSino - LiveSide ????????? wrote ?????? Windows Live Writer ?????? - ?????? - LiveSino - LiveSide ?????????
on Wed, Oct 24 2007 12:08 PM

Pingback from  ?????? Windows Live Writer ?????? - ??????  -  LiveSino - LiveSide ?????????

Sign In Live ID using Live ID, Name/Password, or OpenID
or Join LiveSide to leave a comment!