using System; using System.Configuration; using System.IO; using System.Net; using System.Text; using System.Web; namespace GetRoom { public class Room : IHttpHandler { protected HttpContext Context; public void ProcessRequest(HttpContext context) { Context = context; Context.Response.ContentType = "text/plain"; // allow calling this by Ajax from another web site Context.Response.AddHeader("Access-Control-Allow-Origin", "*"); string roomName = GetParameter("room"); string url = "http://www.imvu.com/rooms/" + roomName; string allHTML; //TODO: Grab the roomId from the HTML, then use the following call // to the API: http://www.imvu.com/api/rooms/room_info.php?room_id=57622347-225 //It returns a lot more data. HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; if (request != null) { request.CookieContainer = new CookieContainer(); Cookie cookie = new Cookie("osCsid", ConfigurationManager.AppSettings["osCsid"]) { Domain = "www.imvu.com" }; request.CookieContainer.Add(cookie); request.UserAgent = ConfigurationManager.AppSettings["Room.UserAgent"]; //"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36"; HttpWebResponse resp = request.GetResponse() as HttpWebResponse; if (resp != null) { Stream receiveStream = resp.GetResponseStream(); if (receiveStream != null) { Encoding encoding = Encoding.GetEncoding(1252); StreamReader readStream = new StreamReader(receiveStream, encoding); char[] read = new char[256]; int count = readStream.Read(read, 0, 256); StringBuilder sb = new StringBuilder(); while (count > 0) { sb.Append(new String(read, 0, count)); count = readStream.Read(read, 0, 256); } readStream.Close(); resp.Close(); allHTML = sb.ToString(); } else { Write(ErrorText); return; } } else { Write(ErrorText); return; } } else { Write(ErrorText); return; } StringReader sr = new StringReader(allHTML); StringBuilder sb2 = new StringBuilder(); string line; bool inJS = false; while ((line = sr.ReadLine()) != null) { if (!inJS) { if (line.StartsWith("var roomInfo = {")) { inJS = true; sb2.AppendLine("{"); } } else { sb2.AppendLine(line); if (line.Trim().StartsWith("}")) break; } } string result = sb2.ToString(); Write(string.IsNullOrWhiteSpace(result) ? ErrorText : result.Replace('\'', '"')); } protected string ErrorText = @"{ 'roomId':'00000000-00', 'owner':'No-one', 'room_name':'Unknown Room', 'desc':'No description', 'max' : '0', 'count' : '0', 'img' : '', 'dl_size' : 'n/a', 'ap' : '0', 'vip' : '0', 'show_ap_name_only' : false, 'whitelist_rating' : 0, 'participants' : [] }".Replace('\'', '"'); public bool IsReusable { get { return false; } } protected virtual string GetParameter(string name) { if (Context != null) { return Context.Request[name]; } throw new ArgumentException("Parameter named '" + name + "' was not passed."); } protected virtual void Write(string text) { if (Context != null) { Context.Response.Write(text); } } } }