Created
January 7, 2013 20:52
-
-
Save anonymous/4478284 to your computer and use it in GitHub Desktop.
code that downloads images - for stackoverflow question: http://stackoverflow.com/questions/14158069/the-request-was-aborted-could-not-create-ssl-tls-secure-channel-in-windows-8-me
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async private void Download_Click_1(object sender, RoutedEventArgs e) | |
{ | |
string[] files = | |
{ | |
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg", | |
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg", | |
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg", | |
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg", | |
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg", | |
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg", | |
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg", | |
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg", | |
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg", | |
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg", | |
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg", | |
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg", | |
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg", | |
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg", | |
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg", | |
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg", | |
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg", | |
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg", | |
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg", | |
"https://si0.twimg.com/profile_images/523560950/discount_1__bigger.jpg" | |
}; | |
List<Task<Byte[]>> tasks = new List<Task<Byte[]>>(); | |
for (int idx = 0; idx < files.Count(); idx++) | |
{ | |
var file = files[idx]; | |
var taskStart = DownloadImageData(new Uri(file), null); | |
tasks.Add(taskStart); | |
if (((idx > 0) && (idx % 5 == 0)) || (idx == files.Count() - 1)) | |
{ | |
await Task.WhenAll(tasks); | |
tasks.Clear(); | |
} | |
} | |
} | |
private async Task<Byte[]> DownloadImageData(Uri url, Object userState) | |
{ | |
Byte[] contentBytes = null; | |
using (HttpClientHandler _handler = new HttpClientHandler()) | |
{ | |
_handler.AllowAutoRedirect = true; | |
_handler.MaxAutomaticRedirections = 4; | |
using (HttpClient httpClient = new HttpClient(_handler)) | |
{ | |
httpClient.DefaultRequestHeaders.ExpectContinue = false; | |
httpClient.DefaultRequestHeaders.Add("Keep-Alive", "false"); | |
try | |
{ | |
// Exception IS THROWN AT LINE BELOW | |
contentBytes = await httpClient.GetByteArrayAsync(url.OriginalString); | |
} | |
catch (Exception ex) | |
{ | |
System.Diagnostics.Debug.WriteLine("Download Failed :" + ex.Message); | |
throw ex; | |
} | |
} | |
_handler.Dispose(); | |
} | |
return contentBytes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment