using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data; using System.Net; public partial class Ajax_Writer : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string connStr = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["skiveConnectionString1"].ToString(); if (Request.QueryString["type"] != null) { string qstring = Request.QueryString["type"].ToString().ToUpper(); switch (qstring) { case "ALLFEEDS": writeAllFeeds(connStr); break; case "ADDFEED": if (Request.QueryString["uri"] != null) saveFeed(connStr, Request.QueryString["uri"].ToString()); else Response.Write("Query string when adding 'uri' is null"); break; case "DELETEFEED": if (Request.QueryString["feed_id"] != null) deleteFeed(connStr, Request.QueryString["feed_id"].ToString()); else Response.Write("Query string when deleting 'feed_id' is null"); break; default: Response.Write("Incorrect query string param:" + Request.QueryString["type"].ToString()); break; } } else Response.Write("Query string 'type' was null"); } private void deleteFeed(string connStr, string feed_id) { bool successful = true; try { using (SqlConnection connection = new SqlConnection(connStr)) { string commandText = "DeleteURI"; SqlCommand command = new SqlCommand(commandText, connection); command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue("feed_id", feed_id); connection.Open(); command.ExecuteNonQuery(); connection.Close(); } } catch (Exception ex) { successful = false; Response.Write("Sorry your feed could not be Feed deleted " + ex.ToString()); } if (successful) Response.Write("Feed deleted"); } private void saveFeed(string connStr, string url) { bool succussful = true; if (!feedIsValid(url)) { Response.Write("Please check if the feed url is valid"); } else { try { using (SqlConnection connection = new SqlConnection(connStr)) { string commandText = "SaveURI"; SqlCommand command = new SqlCommand(commandText, connection); command.CommandType = CommandType.StoredProcedure; //generate a random id for each feed int ticks = Convert.ToInt32(DateTime.Now.Ticks.GetHashCode()); Random feed_id = new Random(ticks); int int_feed_id = feed_id.Next() * ticks; command.Parameters.AddWithValue("hash_id", int_feed_id); command.Parameters.AddWithValue("uri", url); command.Parameters.AddWithValue("title", "no title"); command.Parameters.AddWithValue("updated", DateTime.Now); connection.Open(); command.ExecuteNonQuery(); connection.Close(); } } catch (Exception ex) { if (ex.ToString().IndexOf("Violation of UNIQUE KEY constraint") > 0) Response.Write("This feed has already been added to your reader"); else Response.Write(ex.Message); succussful = false; } if(succussful) Response.Write(url + " Saved"); } } /// /// Returns true if url returns an http 20x status code with 2.5 secs, otherwise returns false /// Modified version of example on: http://carso-owen.blogspot.com/2008/02/check-if-url-is-valid-and-accessible.html /// /// /// private bool feedIsValid(string url) { HttpWebRequest httpReq =(HttpWebRequest)WebRequest.Create(url); httpReq.AllowAutoRedirect =false; httpReq.Method = "HEAD"; httpReq.KeepAlive = false; httpReq.Timeout = 10000; HttpWebResponse httpRes; bool exists = false; try { httpRes = (HttpWebResponse)httpReq.GetResponse(); if(httpRes.StatusCode == HttpStatusCode.OK || httpRes.StatusCode == HttpStatusCode.Found) { exists = true; } } catch (Exception ex) {/*Silent expection is permissable in this instance*/} return exists; } /// /// Outputs a list of the rss feeds in html /// /// Connection string to use to connect to DB private void writeAllFeeds(string connStr) { try { using (SqlConnection connection = new SqlConnection(connStr)) { string commandText = "getAllURIs"; SqlCommand command = new SqlCommand(commandText, connection); command.CommandType = CommandType.StoredProcedure; connection.Open(); using (SqlDataReader dataReader = command.ExecuteReader()) { while (dataReader.Read()) { Response.Write("" + dataReader["feed_uri"] + "" + ", " + dataReader["title"] + ", " + dataReader["updated"] + "Delete
"); } } connection.Close(); } } catch (Exception ex) { Response.Write(ex.Message); } } }