/// <summary> /// Download the thumbnail -- return null if any error (connection, r/w, timeout, etc.) occurs /// </summary> private static Stream SafeDownloadThumbnail(IVideo ivideo) { try { // download the thumbnail PluginHttpRequest pluginHttpRequest = new PluginHttpRequest(ivideo.ThumbnailUrl, HttpRequestCacheLevel.CacheIfAvailable); using (Stream downloadedStream = pluginHttpRequest.GetResponse(5000)) { if (downloadedStream != null) { // transfer it to a memory stream so we don't hold open the network connection // (creating a Bitmap from a Stream requires that you hold open the Stream for // the lifetime of the Bitmap and we clearly don't want to do this for Streams // loaded from the network!) Stream memoryStream = new MemoryStream(); StreamHelper.Transfer(downloadedStream, memoryStream); return(memoryStream); } else { return(null); } } } catch { return(null); } }
private bool ValidatePushpinUrl() { using (new WaitCursor()) { // empty url is ok, means no custom pushpin if (PushpinUrl == String.Empty) { return(true); } // try to validate (take no more than 5 seconds) try { PluginHttpRequest request = new PluginHttpRequest(PushpinUrl, HttpRequestCacheLevel.CacheIfAvailable); using (Stream response = request.GetResponse(5000)) { if (response == null) { return(ShowPushpinValidationError()); } else { return(true); } } } catch (Exception) { return(ShowPushpinValidationError()); } } }
/// <summary> /// Executes a web request against ImageShack and parses the result. /// </summary> /// <param name="url">The URL to execute against.</param> /// <param name="postData">Name/value pairs to send to ImageShack.</param> /// <param name="fileToUpload">Info about a file to upload.</param> /// <returns> /// A parsed XML document with the results. /// </returns> /// <remarks> /// <para> /// An error response from ImageShack looks like this: /// </para> /// <code> /// <links> /// <error id="partial_file_uploaded">Only part of the file was uploaded. Please try to upload file later</error> /// </links> /// </code> /// <para> /// These error responses will be thrown as <see cref="System.InvalidOperationException"/> /// where the message is the text from the error. /// </para> /// </remarks> /// <exception cref="System.NotSupportedException"> /// Thrown if there is no current internet connection. /// </exception> /// <exception cref="InvalidOperationException"> /// Thrown if ImageShack returns an error. /// </exception> protected XDocument ExecuteWebRequest(Uri url, Dictionary <string, string> postData, FileInfo fileToUpload) { if (!PluginHttpRequest.InternetConnectionAvailable) { throw new NotSupportedException("You must have an internet connection avialable to upload to ImageShack."); } HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url.AbsoluteUri); request.AllowAutoRedirect = true; request.Proxy = PluginHttpRequest.GetWriterProxy(); request.Timeout = 120000; // Increase timeout from default 100s to 120s to account for timeouts. if (postData != null || fileToUpload != null) { request.Method = "POST"; request.KeepAlive = true; // Articles on formatting multipart/form-data requests: // http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data // http://www.15seconds.com/Issue/001003.htm // http://blog.dmbcllc.com/2009/11/10/upload-a-file-via-webrequest-using-csharp/ string boundary = CreateFormDataBoundary(); request.ContentType = "multipart/form-data; boundary=" + boundary; Stream requestStream = request.GetRequestStream(); postData.WriteMultipartFormData(requestStream, boundary); if (fileToUpload != null) { string imageMimeType = this.GetImageMimeType(fileToUpload); fileToUpload.WriteMultipartFormData(requestStream, boundary, imageMimeType, "fileupload"); } byte[] endBytes = System.Text.Encoding.UTF8.GetBytes("--" + boundary + "--"); requestStream.Write(endBytes, 0, endBytes.Length); requestStream.Close(); } using (WebResponse response = request.GetResponse()) using (StreamReader reader = new StreamReader(response.GetResponseStream())) { string results = reader.ReadToEnd(); XDocument doc = XDocument.Parse(results); var error = doc.Root.Elements("error").FirstOrDefault(); if (error != null) { throw new InvalidOperationException("An error occurred while communicating with ImageShack: " + error.Value); } return(doc); }; }
/// <summary> /// Download the thumbnail for the specified video (only if it isn't already in our cache) /// </summary> public void DownloadThumbnail(IVideo video) { if (!_thumbnails.Contains(video)) { // if we can get a version of the thumbnail from the cache // then just save this version PluginHttpRequest pluginHttpRequest = new PluginHttpRequest(video.ThumbnailUrl, HttpRequestCacheLevel.CacheOnly); using (Stream cachedStream = pluginHttpRequest.GetResponse()) { if (cachedStream != null) { MemoryStream memoryStream = new MemoryStream(); StreamHelper.Transfer(cachedStream, memoryStream); _thumbnails[video] = new VideoThumbnail(memoryStream); } // otherwise mark it as 'downloading' and enque the download else { _thumbnails[video] = new DownloadingVideoThumbnail(); _workQueue.Enqueue(video); } } } }
internal static Image GetImageFromUrl(string url) { Image theImage; PluginHttpRequest http = new PluginHttpRequest(url); http.AllowAutoRedirect = true; http.CacheLevel = HttpRequestCacheLevel.BypassCache; using (Stream imageStream = http.GetResponse()) { theImage = Image.FromStream(imageStream); } return theImage; }