void ProcessRequest(Request request) { string url = request.Url; Utils.LogDebug("Downloading {0} from: {1}", request.Type, url); object value = null; HttpStatusCode status = HttpStatusCode.OK; string etag = null; try { HttpWebRequest req = MakeRequest(request); using (HttpWebResponse response = (HttpWebResponse)req.GetResponse()) { etag = response.Headers[HttpResponseHeader.ETag]; value = DownloadContent(request, response); } } catch (Exception ex) { if (!(ex is WebException || ex is ArgumentException || ex is UriFormatException || ex is IOException)) { throw; } Utils.LogDebug("Failed to download from: " + url); if (ex is WebException) { WebException webEx = (WebException)ex; if (webEx.Response != null) { status = ((HttpWebResponse)webEx.Response).StatusCode; webEx.Response.Close(); } } } value = CheckIsValidImage(value, url); lock ( downloadedLocker ) { DownloadedItem oldItem; DownloadedItem newItem = new DownloadedItem(value, request.TimeAdded, url, status, etag); if (downloaded.TryGetValue(request.Identifier, out oldItem)) { if (oldItem.TimeAdded > newItem.TimeAdded) { DownloadedItem old = oldItem; oldItem = newItem; newItem = old; } Bitmap oldBmp = oldItem.Data as Bitmap; if (oldBmp != null) { oldBmp.Dispose(); } } downloaded[request.Identifier] = newItem; } }
/// <summary> Returns whether the requested item exists in the downloaded queue. /// If it does, it removes the item from the queue and outputs it. </summary> /// <remarks> If the asynchronous thread failed to download the item, this method /// will return 'true' and 'item' will be set. However, the contents of the 'item' object will be null.</remarks> public bool TryGetItem(string identifier, out DownloadedItem item) { bool success = false; lock ( downloadedLocker ) { success = downloaded.TryGetValue(identifier, out item); if (success) { downloaded.Remove(identifier); } } return(success); }
void DownloadItem( DownloadRequest request ) { string url = request.Url; byte type = request.Type; string dataType = type == 0 ? "image" : (type == 1 ? "string" : "raw"); Utils.LogDebug( "Downloading " + dataType + " from: " + url ); object value = null; try { if( type == 0 ) { byte[] data = client.DownloadData( url ); using( MemoryStream stream = new MemoryStream( data ) ) value = new Bitmap( stream ); } else if( type == 1 ) { value = client.DownloadString( url ); } else if( type == 2 ) { value = client.DownloadData( url ); } Utils.LogDebug( "Downloaded from: " + url ); } catch( Exception ex ) { if( !( ex is WebException || ex is ArgumentException ) ) throw; Utils.LogDebug( "Failed to download from: " + url ); } lock( downloadedLocker ) { DownloadedItem oldItem; DownloadedItem newItem = new DownloadedItem( value, request.TimeAdded, url ); if( downloaded.TryGetValue( request.Identifier, out oldItem ) ) { if( oldItem.TimeAdded > newItem.TimeAdded ) { DownloadedItem old = oldItem; oldItem = newItem; newItem = old; } Bitmap oldBmp = oldItem.Data as Bitmap; if( oldBmp != null ) oldBmp.Dispose(); } downloaded[request.Identifier] = newItem; } }
public bool TryGetItem( string identifier, out DownloadedItem item ) { bool success = false; lock( downloadedLocker ) { success = downloaded.TryGetValue( identifier, out item ); if( success ) { downloaded.Remove( identifier ); } } return success; }
void ProcessRequest( Request request ) { string url = request.Url; Utils.LogDebug( "Downloading {0} from: {1}", request.Type, url ); object value = null; WebException webEx = null; try { HttpWebRequest req = MakeRequest( request ); using( HttpWebResponse response = (HttpWebResponse)req.GetResponse() ) value = DownloadContent( request, response ); } catch( Exception ex ) { if( !( ex is WebException || ex is ArgumentException || ex is UriFormatException ) ) throw; Utils.LogDebug( "Failed to download from: " + url ); if( ex is WebException ) webEx = (WebException)ex; } value = CheckIsValidImage( value, url ); lock( downloadedLocker ) { DownloadedItem oldItem; DownloadedItem newItem = new DownloadedItem( value, request.TimeAdded, url, webEx ); if( downloaded.TryGetValue( request.Identifier, out oldItem ) ) { if( oldItem.TimeAdded > newItem.TimeAdded ) { DownloadedItem old = oldItem; oldItem = newItem; newItem = old; } Bitmap oldBmp = oldItem.Data as Bitmap; if( oldBmp != null ) oldBmp.Dispose(); } downloaded[request.Identifier] = newItem; } }
void DownloadItem( Request request ) { string url = request.Url; byte type = request.Type; string dataType = type == 0 ? "image" : (type == 1 ? "string" : "raw"); Utils.LogDebug( "Downloading " + dataType + " from: " + url ); object value = null; try { if( type == 0 ) { byte[] data = client.DownloadData( url ); using( MemoryStream stream = new MemoryStream( data ) ) value = new Bitmap( stream ); } else if( type == 1 ) { value = client.DownloadString( url ); } else if( type == 2 ) { value = client.DownloadData( url ); } } catch( Exception ex ) { if( !( ex is WebException || ex is ArgumentException ) ) throw; Utils.LogDebug( "Failed to download from: " + url ); } // Mono seems to be returning a bitmap with a native pointer of zero in some weird cases. // We can detect this as every single property access raises an ArgumentException. try { Bitmap bmp = value as Bitmap; if( bmp != null ) { int height = bmp.Height; } } catch (ArgumentException) { Utils.LogDebug( "Failed to download from: " + url ); value = null; } lock( downloadedLocker ) { DownloadedItem oldItem; DownloadedItem newItem = new DownloadedItem( value, request.TimeAdded, url ); if( downloaded.TryGetValue( request.Identifier, out oldItem ) ) { if( oldItem.TimeAdded > newItem.TimeAdded ) { DownloadedItem old = oldItem; oldItem = newItem; newItem = old; } Bitmap oldBmp = oldItem.Data as Bitmap; if( oldBmp != null ) oldBmp.Dispose(); } downloaded[request.Identifier] = newItem; } }