home
products
contribute
download
documentation
forum
Home
Forums
New posts
Search forums
What's new
New posts
All posts
Latest activity
Members
Registered members
Current visitors
Donate
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Search titles only
By:
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
MediaPortal 1
Development
Submit: code patches (MediaPortal/TV-Server/etc.)
Utils: DownLoadImage failed when PageRequest need Header "User-Agent"
Contact us
RSS
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="morfius" data-source="post: 1098805" data-attributes="member: 116650"><p>Finally, I decided to rewrite the entire procedure. You can test with this simple program.</p><p></p><p>[code]</p><p>#region using</p><p>using System;</p><p>using System.Diagnostics;</p><p>using System.Net;</p><p>using System.IO;</p><p>using System.Threading;</p><p>using MediaPortal.GUI.Library;//add ref Core & Utils</p><p>#endregion using</p><p></p><p>/*</p><p> * Developper: mOrfiUs</p><p> * e-mail: apifilmaffinityimdb@gmail.com</p><p> * web: https://sourceforge.net/projects/apifilmaffinityimdb/</p><p> * Forum post: https://forum.team-mediaportal.com/posts/1098788</p><p> * Description: New Core/Util/utils.cs DownLoadImage procedure</p><p> * FIX: Set the necessary headers for proper WebRequest (ex. filmaffinity)</p><p> * Date 2014/09/06</p><p> */</p><p></p><p>namespace TestDLImage</p><p>{</p><p> class TestDLImage</p><p> {</p><p> static void Main(string[] args)</p><p> {</p><p> string[] lDownLoadImages = new string[] {</p><p> "http://fr.web.img2.acsta.net/r_640_600/b_1_d6d6d6/pictures/14/07/04/14/59/301675.jpg", </p><p> "http://fr.web.img5.acsta.net/r_160_240/b_1_d6d6d6/pictures/14/07/04/14/59/301675.jpg", </p><p> "http://st.kinopoisk.ru/images/film_original/450237.jpg", </p><p> "http://image.tmdb.org/t/p/w396/1APNYPrWaZxZnBqnsHTou2MaHnx.jpg", </p><p> "http://pic.filmaffinity.com/Monuments_Men-681632116-large.jpg", </p><p> "http://pics.filmaffinity.com/Monuments_Men-681632116-large.jpg", </p><p> "http://pics.filmaffinity.com/nts_Men-682116-large.jpg", </p><p> "http://ia.media-imdb.com/images/M/MV5BMTQ5NTg5ODk4OV5BMl5BanBnXkFtZTgwODc4MTMzMDE@._V1__SX300.jpg", </p><p> "http://ia.media-imdb.com/images/M/MV5BDk4OV5BMl5BanBnXkFtZTgwODc4MTMzMDE@", </p><p> "http://ia.media-imdb.com/images/M/MV5BMjM4MjU3MDgzNV5BMl5BanBnXkFtZTgwODE3NzcwMTE@"</p><p> };</p><p> int iCount = 0;</p><p> foreach (string DLImage in lDownLoadImages)</p><p> DownLoadImage(DLImage, @"c:\temp" + iCount++.ToString("000") + ".jpg");</p><p> }</p><p></p><p> /// <summary></p><p> /// Download a remote image and save it locally.</p><p> /// If it fails the first time, wait one second and try to download again.</p><p> /// UserAgent and Accept headers are set for correct request.</p><p> /// Unfortunately, synchronous downloads are often problematic, so it is necessary to correctly handle errors.</p><p> /// </summary></p><p> /// <param name="strURL">remote image to download</param></p><p> /// <param name="strFile">local image to save</param></p><p> /// <param name="secondTry">bool used for the recursive call, after first fail</param></p><p> public static void DownLoadImage(string strURL, string strFile, bool secondTry = false)</p><p> {</p><p> if (string.IsNullOrEmpty(strURL) || string.IsNullOrEmpty(strFile))</p><p> return;</p><p> Uri uri = new Uri(strURL);</p><p> HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(uri);</p><p> wReq.Proxy.Credentials = CredentialCache.DefaultCredentials;</p><p> wReq.UserAgent = "Mozilla/8.0 (compatible; MSIE 9.0; Windows NT 6.1; .NET CLR 1.0.3705;)";</p><p> wReq.Accept = "*/*";</p><p> //should check the results before continue</p><p> //if wr.StatusCode == HttpStatusCode.OK</p><p> //but don't have reason if is a void procedure</p><p> try</p><p> {</p><p> using (HttpWebResponse wr = (HttpWebResponse)wReq.GetResponse())</p><p> using (BinaryReader br = new BinaryReader(wr.GetResponseStream()))</p><p> using (FileStream fs = new FileStream(strFile, FileMode.OpenOrCreate, FileAccess.Write))</p><p> {</p><p> byte[] b = new byte[1024 * 128];//Should we increase the buffer size?. We live in 2014</p><p> int bRead;</p><p> do</p><p> {</p><p> bRead = br.Read(b, 0, b.Length);</p><p> fs.Write(b, 0, bRead);</p><p> } while (bRead != 0);</p><p> }</p><p> }</p><p> catch (System.Net.WebException eWebException)</p><p> {</p><p> Log.Info("Utils: DownLoadImage {1} failed: {0}", eWebException.Message + (secondTry ? " second try also fail" : string.Empty), strURL);</p><p> //after second fail, return</p><p> if (secondTry)</p><p> return;</p><p> //just wait a second, and then try again</p><p> Thread.Sleep(1000);</p><p> DownLoadImage(strURL, strFile, true);</p><p> }</p><p> return;</p><p> }</p><p></p><p> }</p><p>}</p><p>[/code]</p><p>Greetings from Spain!!!</p></blockquote><p></p>
[QUOTE="morfius, post: 1098805, member: 116650"] 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) return; //just wait a second, and then try again Thread.Sleep(1000); DownLoadImage(strURL, strFile, true); } return; } } } [/code] Greetings from Spain!!! [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
MediaPortal 1
Development
Submit: code patches (MediaPortal/TV-Server/etc.)
Utils: DownLoadImage failed when PageRequest need Header "User-Agent"
Contact us
RSS
Top
Bottom