/// <summary> /// Asynchronously retrieves an asset from the web given its URL. /// </summary> /// <param name="assetUrl">The URL of the asset to be retrieved.</param> /// <param name="config">The application configuration instance.</param> /// <returns>Asset data, as an array of bytes.</returns> /// <exception cref="ArgumentNullException">Thrown if /// <paramref name="assetUrl"/> or <paramref name="config"/>is null. /// </exception> public static async Task <byte[]> GetAssetDataFromUrlAsync(Uri assetUrl, AdsConfig config) { if (assetUrl == null) { throw new ArgumentNullException(nameof(assetUrl)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } WebRequest request = HttpUtilities.BuildRequest(assetUrl.AbsoluteUri, "GET", config); WebResponse response = await request.GetResponseAsync(); MemoryStream memStream = new MemoryStream(); using (Stream responseStream = response.GetResponseStream()) { await responseStream.CopyToAsync(memStream); } return(memStream.ToArray()); }
/// <summary> /// Retrieves an asset from the web given its URL. /// </summary> /// <param name="assetUrl">The URL of the asset to be retrieved.</param> /// <param name="config">The application configuration instance.</param> /// <returns>Asset data, as an array of bytes.</returns> /// <exception cref="ArgumentNullException">Thrown if /// <paramref name="assetUrl"/> or <paramref name="config"/>is null. /// </exception> public static byte[] GetAssetDataFromUrl(Uri assetUrl, AdsConfig config) { if (assetUrl == null) { throw new ArgumentNullException("assetUrl"); } if (config == null) { throw new ArgumentNullException("config"); } WebRequest request = HttpUtilities.BuildRequest(assetUrl.AbsoluteUri, "GET", config); WebResponse response = request.GetResponse(); MemoryStream memStream = new MemoryStream(); using (Stream responseStream = response.GetResponseStream()) { responseStream.CopyTo(memStream); } return(memStream.ToArray()); }