<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Text;
using System.Xml;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext c) {
c.Response.ContentType = "text/xml";
XmlTextWriter xmlWrite = new XmlTextWriter(c.Response.OutputStream, Encoding.UTF8);
xmlWrite = new XmlTextWriter(c.Response.OutputStream, Encoding.UTF8);
xmlWrite.WriteStartDocument();
xmlWrite.WriteStartElement("SearchSuggestion");
xmlWrite.WriteAttributeString("xmlns", "http://schemas.microsoft.com/Search/2008/suggestions");
xmlWrite.WriteElementString("Query", c.Request.QueryString["q"]);
xmlWrite.WriteStartElement("Section");
xmlWrite.WriteStartElement("Separator");
xmlWrite.WriteAttributeString("title", "My Visual Suggestions");
xmlWrite.WriteEndElement();
if (c.Request.QueryString["q"] != null)
{
FlickrNet.Flickr flickr = new FlickrNet.Flickr();
FlickrNet.PhotoSearchOptions options = new FlickrNet.PhotoSearchOptions();
options.Text = c.Server.UrlDecode(c.Request.QueryString["q"]);
options.PerPage = 9;
options.Page = 1;
options.SortOrder = FlickrNet.PhotoSearchSortOrder.DatePostedDesc;
FlickrNet.Photos photos = flickr.PhotosSearch(options);
if (photos.PhotoCollection != null)
{
foreach (FlickrNet.Photo p in photos.PhotoCollection)
{
xmlWrite.WriteStartElement("Item");
xmlWrite.WriteElementString("Text", p.Title);
xmlWrite.WriteStartElement("Image");
xmlWrite.WriteAttributeString("source", p.SquareThumbnailUrl);
xmlWrite.WriteAttributeString("alt", p.Title);
xmlWrite.WriteAttributeString("width", "75");
xmlWrite.WriteAttributeString("height", "75");
xmlWrite.WriteEndElement();
xmlWrite.WriteElementString("Url", p.WebUrl);
xmlWrite.WriteEndElement();
}
}
}
xmlWrite.WriteEndElement();
xmlWrite.WriteEndElement();
xmlWrite.Flush();
xmlWrite.Close();
}
public bool IsReusable {
get {
return false;
}
}
}