private void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e) { var url = string.Format("http://www.youtube.com/watch?v={0}", e.Argument); _errorMessage = string.Empty; var request = (HttpWebRequest)WebRequest.Create(url); var response = (HttpWebResponse)request.GetResponse(); var responseStream = response.GetResponseStream(); if (responseStream == null) { _errorMessage = "Error while reading response from YouTube."; return; } var source = new StreamReader(responseStream, Encoding.UTF8).ReadToEnd(); var found = source.IndexOf("x-flv"); while (!source.Substring(found, 4).Equals("http")) found--; source = source.Remove(0, found); source = HttpUtility.UrlDecode(source); source = HttpUtility.UrlDecode(source); //Twice _errorMessage = source.Substring(0,source.IndexOf("&quality")); }
public XS1ActuatorList getXS1ActuatorList(String XS1_URL, String Username, String Password) { // TODO: more error handling !!! // check if we already cached something if (ActuatorListCache != null) { if ((DateTime.Now - LastActuatorListUpdated).TotalMinutes < ConfigurationCacheMinutes) return ActuatorListCache; } // now we got the parameters, we need to find out which actors and which functions shall be called WebRequest wrGetURL = WebRequest.Create("http://" + XS1_URL + "/control?user="******"&pwd=" + Password + "&callback=actorlist&cmd=get_list_actuators"); String _UsernameAndPassword = Username + ":" + Password; String _AuthorizationHeader = "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(_UsernameAndPassword)); wrGetURL.Credentials = new NetworkCredential(Username, Password); wrGetURL.Headers.Add("Authorization", _AuthorizationHeader); HttpWebResponse response = (HttpWebResponse)wrGetURL.GetResponse(); // check for eventual errors if (response.StatusCode != HttpStatusCode.OK) { // TODO: refactor to correct http response codes return null; } // we will read data via the response stream String actuator_config_json = new StreamReader(response.GetResponseStream()).ReadToEnd(); JavaScriptSerializer ser = new JavaScriptSerializer(); ser.MaxJsonLength = 20000000; // remove the javascript callback/definitions actuator_config_json = actuator_config_json.Replace("actorlist(", ""); actuator_config_json = actuator_config_json.Remove(actuator_config_json.Length - 4, 4); // deserialize the XS1 configuration json stream ActuatorListCache = ser.Deserialize<XS1ActuatorList>(actuator_config_json); LastActuatorListUpdated = DateTime.Now; return ActuatorListCache; }
public String SetStateActuatorPreset(String XS1_URL, String Username, String Password, Int32 ActuatorID, Int32 PresetID) { // TODO: more error handling !!! WebRequest wrGetURL = WebRequest.Create("http://" + XS1_URL + "/control?user="******"&pwd=" + Password + "&callback=setstate&cmd=set_state_actuator&number="+ActuatorID+"&function="+PresetID); String _UsernameAndPassword = Username + ":" + Password; String _AuthorizationHeader = "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(_UsernameAndPassword)); wrGetURL.Credentials = new NetworkCredential(Username, Password); wrGetURL.Headers.Add("Authorization", _AuthorizationHeader); HttpWebResponse response = (HttpWebResponse)wrGetURL.GetResponse(); // check for eventual errors if (response.StatusCode != HttpStatusCode.OK) { // TODO: refactor to correct http response codes return null; } // we will read data via the response stream String actuator_config_json = new StreamReader(response.GetResponseStream()).ReadToEnd(); JavaScriptSerializer ser = new JavaScriptSerializer(); ser.MaxJsonLength = 20000000; // remove the javascript callback/definitions actuator_config_json = actuator_config_json.Replace("setstate(", ""); actuator_config_json = actuator_config_json.Remove(actuator_config_json.Length - 4, 4); return ""; }