[Pending] Utils: DownLoadImage failed when PageRequest need Header "User-Agent" (1 Viewer)

morfius

Portal Pro
November 10, 2011
40
48
58
Valladolid
Home Country
Spain Spain
I'm still not familiar with the peculiarities of the MediaPortal code. My apologies. And even less with GitHub. More apologies. I prefer to answer here

Code:
[Obsolete("Method1 is deprecated, please use Method2 instead.")]
This entry point is not used anymore, so is more simple....delete it?

Usually it's good to set some decent timeout like 20 or 30 seconds. At least in Java world defaults can be very long like minutes or even infinite so I would put wReq.Timeout = 20000 after wReq.Accept
You are rigth. This question stuck in my inkwell. Fixed.

Looks good to me
Thanks

using (Stream response = wReq.GetResponse().GetResponseStream()) ?.
This have been tested with bad results. I prefer to keep it that way, but read again c) from my initial solutions, at the start of this post. (a little joke)

At this time, the code is as follows
Code:
    [Obsolete("This entry point is deprecated, please use DownLoadImage(string strURL, string strFile) instead")]    
    public static void DownLoadImage(string strURL, string strFile, System.Drawing.Imaging.ImageFormat imageFormat)
    {
      if ((string.IsNullOrEmpty(strURL)) || (string.IsNullOrEmpty(strFile)))
        return;
      DownLoadImage(strURL, strFile);
    }

    /// <summary>
    /// Download a remote image and save it locally.
    /// UserAgent and Accept headers are set for correct request.
    /// Error handling is not responsibility of this method and should be done by method caller as seen fit
    /// see https://forum.team-mediaportal.com/posts/1096988 for more details
    /// </summary>
    /// <param name="strURL">remote image to download</param>
    /// <param name="strFile">local image to save</param>
    public static void DownLoadImage(string strURL, string strFile)
    {
      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 = "*/*";
      wReq.Timeout = 20000;

      using (HttpWebResponse wr = (HttpWebResponse)wReq.GetResponse())
      {
        using (Stream br = wr.GetResponseStream())
        {
          using (FileStream fs = new FileStream(strFile, FileMode.OpenOrCreate, FileAccess.Write))
          {
            br.CopyTo(fs);
          }
        }
      }
    }

This code it's just a drop in the ocean, and far from the complexity of developing a mobile driver to manage MediaPortal, but this procedure is used a lot of times.
My only intention is that, this method work efficiently and quickly (of course without errors)

Greetings from Spain!
 
Last edited:

morfius

Portal Pro
November 10, 2011
40
48
58
Valladolid
Home Country
Spain Spain
It's always good that new blood are around :)
Hi Sebastiii. Sorry, but just read now your post.. My blood its fine (a little of cholesterol) but really... it's not new (far from it) ;) I think my better contribution to this project are about the information of films and tv shows, and perhaps, any isolated method or function needed for some part of the code.
I understand all members here are pro-actives. I am technician engineer, developer (level medium), system administrator (many years) and considerable knowledges about SQL database.
There are a Joke:..
.- Who has some music knowledges?
.- Me, sir.
.- You. Load the piano of the Captain!
I don't have any problems to Load the Piano, or to perform minor tasks.
At this time, i'd like finish these tasks:
One Convert HOWTO post to Wiki (Movie Info Grabbers)
Two: This post


Regards!
 

morfius

Portal Pro
November 10, 2011
40
48
58
Valladolid
Home Country
Spain Spain
Hi Sebastiii. At this moment, in Github
Closed with unmerged commits.
So... The last version of the proposed routine is not merge in the code.
I think it should include the last version before closing this post.
I am in your hands to make any improvementes or to make any changes you want to.
Greetings from Spain.
 

Edalex

Community Plugin Dev
  • Premium Supporter
  • January 3, 2008
    2,955
    1,264
    Saratov
    Home Country
    Russian Federation Russian Federation
    Nice to see new contributions to MP!
    I assume that error with downloading file was with scrapper so why don't we modify scraper itself instead of rewriting mediaportal code? Scrapper in C# so downloading with any webclient/webrequest is possible.
    Also I want to say that setting headers in download method is not so flexible. Some sites could require special headers i.e. "MediaPortal 1.1.0".
    So I think it will be better to have another method with more parameters like headers, gzip encoding and so on.
     

    Users who are viewing this thread

    Top Bottom