Archive for April, 2005

Ebay and RSS

I think it’d be cool if Ebay offered rss watch lists for
searches.  I’d like to be able to see stuff that I’m watching
without having to remember to log into Ebay.  Come to think of it,
I think basically any site that has a search or headlines should also
expose the content in rss format.  People who are concerned about
copyright infringement forget that content can get scraped pretty
easily anyways, you might as well let people develop some cool apps for
your content, it only helps your brand awareness.

UPDATE: I didn’t realize Brendan had just posted a something related. Check it out.

Ajax POST request

As kind of a followup to my post about how to redirect XmlHttpRequests outside your domain, which was more popular that I had expected, I want to show you how to accept posted data from your Ajax requests. 

When you are opening a request, you have the option of a “GET” or a “POST”, there are many limitations of just using “GET”, which I won’t discuss here.  We are using Ajax to run our custom content management system, where users had a WYSIWYG editor to edit their content.  You certainly cannot use a “GET” request to send the html content back to the server, so this is a great time to “POST” the data instead. 

What we wanted to do was essentially post an xml document back to the server, and xml document that would be constructed in javascript, and then submitted for processing on the server.  Inside this xml document we would put the WYSIWYG editor value, as well as other data from the form.  So we packaged up the data in javascript in the usual way and then we posted the data.  Below I have the code that I use to retrieve the data and populate an XmlDocument using that data.

    1 #region Using directives

    2 

    3 using System;

    4 using System.IO;

    5 using System.Xml;

    6 using System.Collections.Specialized;

    7 

    8 #endregion

    9 

   10 namespace XmlPost

   11 {

   12     public class XmlPostPage : System.Web.UI.Page

   13     {

   14         protected override void OnInit(EventArgs e)

   15         {

   16             Load += new EventHandler(Page_Load);

   17         }

   18 

   19         private void Page_Load(object sender, System.EventArgs e)

   20         {

   21             string postedData = “”;

   22             //

   23             // Read the xml string that was posted in the Request.InputStream

   24             //

   25             using( StreamReader sr = new StreamReader(Request.InputStream) )

   26             {

   27                 postedData = sr.ReadToEnd();

   28             }

   29 

   30             //

   31             // Load the posted xml into an XmlDocument to

   32             // do whatever actions need to be done

   33             //

   34             XmlDocument xd = new XmlDocument();

   35             xd.LoadXml(postedData);

   36 

   37             //

   38             // …..continue processing xml data as needed

   39             //

   40 

   41         }

   42     }

   43 }

So using this method allows you to post just about anything you normally would, and you still get the benefit of the asynchronous behavior.  What I want to try next is “POST”-ing a file input over an Ajax request, I think you could make a really nice looking file uploader by uploading the file asynchronously and showing the user a nice “Waiting….” message.

Writing and the Office API

Its funny how writing can help you clarify your thoughts. I’m working
on a client application that integrates with Outlook 2003, and I was
getting frustrated with the Office api, as its definitely not as clear
or rich as the .net framework BCL.  For instance, I still don’t
know if there is a way to open a contact item by simply specifying its
EntryID, the unique id that is assigned to objects in Office; instead
you have to open a contact folder, and iterate over the items until you
find the one you want, it just seems like they went out of their way to
make it difficult.

But I digress, this is not meant to be a post that rips on the Office
api, because its actually really powerful and useful.  I sat down
to write a post a few days ago about how the Office api sucks and how
there are hardly any decent code samples, documentation, etc. 
Each time I tried to make a point of how the api sucks, however, I
found myself thinking “well its really not that bad, its just new to
me, and in actuality, its pretty good.”  So I found that I really
had no solid reason for hating on the Office api, and I changed my mind
about the whole thing.  To be perfectly honest, since I just
started this app this week, I’ve actually got a fairly good grasp on
the api after only a few days, which just reinforces the idea that the api is well thought out and intuitive.

So, thanks to the Office team for making it easy for me to integrate, I appreciate it :-)

Adobe buys Macromedia

Just a little public service announcement :)

Adobe buys Macromedia

Cross Domain XmlHttpRequests (Ajax)

One problem you run into when using client side xml calls is the
issue of getting some xml from a different domain.  Making a
cross domain request is simply denied in Firefox, and I believe it is
denied in SP2 IE, however pre-SP2 you were just alerted to the fact
that a cross domain request was being made.  On some of our pages
we want to include Yahoo news feeds that
relate to the content on the page.  Since we couldn’t just make
the request (Response.Redirects also do not work), I wrote a little
page that takes in another url as a querystring parameter, makes its
own web request to that page, and streams the content down to the
client.  This allows you to get any feed from any domain you want,
so it can come in pretty handy for the scenario I described
above.  Here is the code from the intermediate page: 

UPDATE:
Sorry for the poor formatting, I guess I’m not using CopySourceAsHTML
in the correct way. If you view the post on my blog page, the
formatting is intact.

    1 #region Using directives

    2 

    3 using System;

    4 using System.Text;

    5 using System.Xml;

    6 

    7 #endregion

    8 

    9 namespace MyApp.Web.Services

   10 {

   11     public class Redirecter : System.Web.UI.Page

   12     {

   13         protected override void OnInit(EventArgs e)

   14         {

   15             Load += new EventHandler(Page_Load);

   16         }

   17 

   18         void Page_Load(object sender, EventArgs e)

   19         {

   20             XmlDocument responseDoc;

   21             string url = Request.QueryString[“Url”];

   22 

   23             if (url != null && url.Length > 0)

   24                 responseDoc = GetExternalFeed(url);

   25             else

   26                 responseDoc = GetError();

   27 

   28             Response.ContentType = “text/xml”;

   29             Response.Write(responseDoc.InnerXml);

   30             Response.End();

   31         }

   32 

   33         private XmlDocument GetExternalFeed(string url)

   34         {

   35             string u = Server.UrlDecode(url);

   36             System.Net.WebRequest wr = System.Net.WebRequest.Create(u);

   37             XmlDocument doc = new XmlDocument();

   38             doc.Load( wr.GetResponse().GetResponseStream() );

   39             return doc;

   40         }

   41 

   42         private XmlDocument GetError()

   43         {

   44             XmlDocument doc = new XmlDocument();

   45             doc.LoadXml(Invalid url);

   46             return doc;

   47         }

   48 

   49     }

   50 }

You’ll notice in the “GetExternalFeed” method, I’m UrlDecoding the
querystring parameter to create a valid url from the string.  Make
sure to use the javascript “escape
function on your querystring parameter before making the request,
otherwise any “&” in the url will be lost and your url will be
invalid.

So thats all there is to it, now you can get xml feeds from anywhere on the web!

Book Review

I’ve been reading “Head First
Design Patterns”
for about a month now.  I’d heard that design
patterns are kind of a dry subject, and thats completely true. This book
is definitely the best one I’ve seen as far as presenting the material
in a way that keeps you interested, but I still can’t seem to read it
for an extended period of time.  It is interesting to recognize
some of the patterns they describe in the .Net framework, so I’m gonna
keep plugging away at it, the nice thing is that you can pick one
pattern to learn at a time so you can read a chapter and re-read it
until you’ve got it. I like the way the book is organized very
discretely (not discreetly).

Blogger’s Block

I know I haven’t posted in awhile,
I think I may have some “blogger’s block” or something. :) It seems
that blogging is habit forming, much the same way that working out is
for me.  If I don’t go to the gym for a few days in a row, its
much harder to start going again, whereas if I go everyday, its easy to
continue to keep going.  If I don’t post for a few days, it seems
more difficult to write that next post, as opposed to when I post
multiple times a day. But I’ve got some ideas for a few posts, and a
series or two, so I’m gonna try to write some this weekend.