0

In the top of form1 i did

WebBrowser webbrowser1;

Then in the constructor

webbrowser1 = new WebBrowser();               webbrowser1.Navigate("http://www.ims.gov.il/IMS/tazpiot/RainRadar.htm");
webbrowser1.DocumentCompleted += webbrowser1_DocumentCompleted;

Then in the DocumentCompleted event

void webbrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (e.Url.AbsoluteUri != webbrowser1.Url.AbsoluteUri)
            {
                return;
            }
            IHTMLDocument2 doc = (IHTMLDocument2)webbrowser1.Document.DomDocument;
            IHTMLControlRange imgRange = (IHTMLControlRange)((HTMLBody)doc.body).createControlRange();

            foreach (IHTMLImgElement img in doc.images)
            {
                imgRange.add((IHTMLControlElement)img);

                imgRange.execCommand("Copy", false, null);

                using (Bitmap bmp = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap))
                {
                    bmp.Save(@"E:\newimages\" + img.nameProp);
                }
            }
        }

I'm getting on the hard disk 20 images saved. But not the one i needed. The one i need to save from this page in the webBrowser is the image in this link: http://www.ims.gov.il/Ims/Pages/RadarImage.aspx?Row=9&TotalImages=10&LangID=1&Location=

The radar it self image. I can make save as... And i can use like i'm doing today WebClient... But WebClient is not working all the time. So i'm trying to surf to the site and get the image of the radar from the webBrowser. But without success.

UPDATE

This is my webclient how i'm downloading the radar image today:

private void fileDownloadRadar()
        {
            if (Client.IsBusy == true)
            {
                Client.CancelAsync();
            }
            else
            {
                try
                {

                    Client.DownloadFileAsync(myUri, Path.Combine(combinedTemp)); 
                }
                catch(Exception errors)
                {
                    string errors2 = errors.ToString();
                }

            }
        }

Client is WebClient

Now i'm going to the directory of where it was downloaded combinedTemp and i see that the downloaded file is 0kb empty.

The url in myUri is:

http://www.ims.gov.il/Ims/Pages/RadarImage.aspx?Row=9&TotalImages=10&LangID=1&Location=

When i put this link in my chrome and surf to it i see the latest radar image but when i'm looking in my hard disk it's 0kb the downloaded file.

And i will add this is not happening now but some time ago some months ago i had another problem in some cases not all the time but in many times when i downloaded the image i had another error i even have open question about it one year ago : Why i'm getting exception: Too many automatic redirections were attempted on webclient?

So in general it's two problems but now the problem is that the downloaded file is 0kb.

This is the Client completed event:

private void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                try
                {
                    client_Error_counter = client_Error_counter + 1;
                    if (client_Error_counter == 5)
                    {
                        Logger.Write("Tried to download radar image file " + client_Error_counter + " times without success");
                        Logger.Write("Error Tried to download radar image file : " + e.Error);
                        client_Error_counter = 0;
                    }
                    else
                    {

                        Client.DownloadFileAsync(myUri, Path.Combine(combinedTemp));
                        Logger.Write("Times tried to download radar image file: " + client_Error_counter);
                        Logger.Write("Error Tried to download radar image file : " + e.Error);
                    }
                }
                catch(Exception errors)
                {
                    string myerr = errors.ToString();
                }
            }

And i see now i missed it that when it's getting to the DownloadFileCompleted event there is an error:

e.Error show: e.Error = {"Too many automatic redirections were attempted."}

StackTRace:

at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result)
   at System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)

This is why i tried to use the WebBrowser. I can't find a way to solve this "Too many automatic redirections were attempted" problem.

That's why i see the file 0kb on my hard disk.

Another thing is today few hours ago it did download fine some radar images but now it's making this error again.

Community
  • 1
  • 1
  • When `WebClient` doesn't work, what error or result do you get? Could you post a snippet of your `WebClient` code? – erdomke Jan 26 '16 at 02:16
  • erdomke i updated my question now. You can see the WebClient part. And when it's getting to the DownloadFileCompleted event of the webclient i'm getting there the error "Too many automatic redirections were attempted" error i can't solve for long time. In my question i also added a link to a question i asked about it a year ago. And still i can't figure out how to solve it. That's why i tried to use WebBrowser. – Falcons Myman Jan 26 '16 at 03:19
  • When i surf to the site to the link in the variable myUri i can surf and see the latest radar image no problem but in my program when i'm using WebClient to download it i'm getting this error "Too many automatic redirections were attempted". – Falcons Myman Jan 26 '16 at 03:19
  • The method fileDownloadRadar() i'm calling it from a timer every 5 or 10 seconds i tried a year ago and also today to change it to 30 seconds and even to 15 minutes. But same error in some cases the download of the file was success fine but in most of the cases or many cases i'm getting the error "Too many automatic redirections were attempted". – Falcons Myman Jan 26 '16 at 03:21
  • Try seeing what the difference is between the web browser and a webclient call is by using a till like Fiddler to intercept the traffic. It might be as simple as making sure there is a cookie container on your WebClient. – erdomke Jan 26 '16 at 04:06

0 Answers0