Normal
Finally, I decided to rewrite the entire procedure. You can test with this simple program.[code]#region usingusing System;using System.Diagnostics;using System.Net;using System.IO;using System.Threading;using MediaPortal.GUI.Library;//add ref Core & Utils#endregion using/* * Developper: mOrfiUs * e-mail: apifilmaffinityimdb@gmail.com * web: https://sourceforge.net/projects/apifilmaffinityimdb/ * Forum post: https://forum.team-mediaportal.com/posts/1098788 * Description: New Core/Util/utils.cs DownLoadImage procedure * FIX: Set the necessary headers for proper WebRequest (ex. filmaffinity) * Date 2014/09/06 */namespace TestDLImage{ class TestDLImage { static void Main(string[] args) { string[] lDownLoadImages = new string[] { "http://fr.web.img2.acsta.net/r_640_600/b_1_d6d6d6/pictures/14/07/04/14/59/301675.jpg", "http://fr.web.img5.acsta.net/r_160_240/b_1_d6d6d6/pictures/14/07/04/14/59/301675.jpg", "http://st.kinopoisk.ru/images/film_original/450237.jpg", "http://image.tmdb.org/t/p/w396/1APNYPrWaZxZnBqnsHTou2MaHnx.jpg", "http://pic.filmaffinity.com/Monuments_Men-681632116-large.jpg", "http://pics.filmaffinity.com/Monuments_Men-681632116-large.jpg", "http://pics.filmaffinity.com/nts_Men-682116-large.jpg", "http://ia.media-imdb.com/images/M/MV5BMTQ5NTg5ODk4OV5BMl5BanBnXkFtZTgwODc4MTMzMDE@._V1__SX300.jpg", "http://ia.media-imdb.com/images/M/MV5BDk4OV5BMl5BanBnXkFtZTgwODc4MTMzMDE@", "http://ia.media-imdb.com/images/M/MV5BMjM4MjU3MDgzNV5BMl5BanBnXkFtZTgwODE3NzcwMTE@" }; int iCount = 0; foreach (string DLImage in lDownLoadImages) DownLoadImage(DLImage, @"c:\temp" + iCount++.ToString("000") + ".jpg"); } /// <summary> /// Download a remote image and save it locally. /// If it fails the first time, wait one second and try to download again. /// UserAgent and Accept headers are set for correct request. /// Unfortunately, synchronous downloads are often problematic, so it is necessary to correctly handle errors. /// </summary> /// <param name="strURL">remote image to download</param> /// <param name="strFile">local image to save</param> /// <param name="secondTry">bool used for the recursive call, after first fail</param> public static void DownLoadImage(string strURL, string strFile, bool secondTry = false) { if (string.IsNullOrEmpty(strURL) || string.IsNullOrEmpty(strFile)) return; Uri uri = new Uri(strURL); HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(uri); wReq.Proxy.Credentials = CredentialCache.DefaultCredentials; wReq.UserAgent = "Mozilla/8.0 (compatible; MSIE 9.0; Windows NT 6.1; .NET CLR 1.0.3705;)"; wReq.Accept = "*/*"; //should check the results before continue //if wr.StatusCode == HttpStatusCode.OK //but don't have reason if is a void procedure try { using (HttpWebResponse wr = (HttpWebResponse)wReq.GetResponse()) using (BinaryReader br = new BinaryReader(wr.GetResponseStream())) using (FileStream fs = new FileStream(strFile, FileMode.OpenOrCreate, FileAccess.Write)) { byte[] b = new byte[1024 * 128];//Should we increase the buffer size?. We live in 2014 int bRead; do { bRead = br.Read(b, 0, b.Length); fs.Write(b, 0, bRead); } while (bRead != 0); } } catch (System.Net.WebException eWebException) { Log.Info("Utils: DownLoadImage {1} failed: {0}", eWebException.Message + (secondTry ? " second try also fail" : string.Empty), strURL); //after second fail, return if (secondTry) return; //just wait a second, and then try again Thread.Sleep(1000); DownLoadImage(strURL, strFile, true); } return; } }}[/code]Greetings from Spain!!!
Finally, I decided to rewrite the entire procedure. You can test with this simple program.
[code]
#region using
using System;
using System.Diagnostics;
using System.Net;
using System.IO;
using System.Threading;
using MediaPortal.GUI.Library;//add ref Core & Utils
#endregion using
/*
* Developper: mOrfiUs
* e-mail: apifilmaffinityimdb@gmail.com
* web: https://sourceforge.net/projects/apifilmaffinityimdb/
* Forum post: https://forum.team-mediaportal.com/posts/1098788
* Description: New Core/Util/utils.cs DownLoadImage procedure
* FIX: Set the necessary headers for proper WebRequest (ex. filmaffinity)
* Date 2014/09/06
*/
namespace TestDLImage
{
class TestDLImage
static void Main(string[] args)
string[] lDownLoadImages = new string[] {
"http://fr.web.img2.acsta.net/r_640_600/b_1_d6d6d6/pictures/14/07/04/14/59/301675.jpg",
"http://fr.web.img5.acsta.net/r_160_240/b_1_d6d6d6/pictures/14/07/04/14/59/301675.jpg",
"http://st.kinopoisk.ru/images/film_original/450237.jpg",
"http://image.tmdb.org/t/p/w396/1APNYPrWaZxZnBqnsHTou2MaHnx.jpg",
"http://pic.filmaffinity.com/Monuments_Men-681632116-large.jpg",
"http://pics.filmaffinity.com/Monuments_Men-681632116-large.jpg",
"http://pics.filmaffinity.com/nts_Men-682116-large.jpg",
"http://ia.media-imdb.com/images/M/MV5BMTQ5NTg5ODk4OV5BMl5BanBnXkFtZTgwODc4MTMzMDE@._V1__SX300.jpg",
"http://ia.media-imdb.com/images/M/MV5BDk4OV5BMl5BanBnXkFtZTgwODc4MTMzMDE@",
"http://ia.media-imdb.com/images/M/MV5BMjM4MjU3MDgzNV5BMl5BanBnXkFtZTgwODE3NzcwMTE@"
};
int iCount = 0;
foreach (string DLImage in lDownLoadImages)
DownLoadImage(DLImage, @"c:\temp" + iCount++.ToString("000") + ".jpg");
}
/// <summary>
/// Download a remote image and save it locally.
/// If it fails the first time, wait one second and try to download again.
/// UserAgent and Accept headers are set for correct request.
/// Unfortunately, synchronous downloads are often problematic, so it is necessary to correctly handle errors.
/// </summary>
/// <param name="strURL">remote image to download</param>
/// <param name="strFile">local image to save</param>
/// <param name="secondTry">bool used for the recursive call, after first fail</param>
public static void DownLoadImage(string strURL, string strFile, bool secondTry = false)
if (string.IsNullOrEmpty(strURL) || string.IsNullOrEmpty(strFile))
return;
Uri uri = new Uri(strURL);
HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(uri);
wReq.Proxy.Credentials = CredentialCache.DefaultCredentials;
wReq.UserAgent = "Mozilla/8.0 (compatible; MSIE 9.0; Windows NT 6.1; .NET CLR 1.0.3705;)";
wReq.Accept = "*/*";
//should check the results before continue
//if wr.StatusCode == HttpStatusCode.OK
//but don't have reason if is a void procedure
try
using (HttpWebResponse wr = (HttpWebResponse)wReq.GetResponse())
using (BinaryReader br = new BinaryReader(wr.GetResponseStream()))
using (FileStream fs = new FileStream(strFile, FileMode.OpenOrCreate, FileAccess.Write))
byte[] b = new byte[1024 * 128];//Should we increase the buffer size?. We live in 2014
int bRead;
do
bRead = br.Read(b, 0, b.Length);
fs.Write(b, 0, bRead);
} while (bRead != 0);
catch (System.Net.WebException eWebException)
Log.Info("Utils: DownLoadImage {1} failed: {0}", eWebException.Message + (secondTry ? " second try also fail" : string.Empty), strURL);
//after second fail, return
if (secondTry)
//just wait a second, and then try again
Thread.Sleep(1000);
DownLoadImage(strURL, strFile, true);
[/code]
Greetings from Spain!!!