1

Please see this thread:

In this thread we have the same issue answered about do...while. But what about foreach? Meaning how can we retry a try statement in try/catch inside this foreach with another proxy (another number of proxy_line_num integer):

foreach (string link_1 in links_with_kid)
{
   try
   {
      getData = "";
      req = (HttpWebRequest)WebRequest.Create(link_1);
      req.Method = "GET";
      req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
      req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0";
      req.ContentType = "text/html; charset=utf-8";
      req.Referer = "http://www.example.com/";
      req.KeepAlive = true;
      req.Timeout = 25000;

      if (useproxy)
      {
         string[] Ok_ip_port_ar = list_lines_GPL[proxy_line_num].Split(':');
         proxy_line_num++;

         if (proxy_line_num == list_lines_GPL.Count)
         {
            proxy_line_num = 0;
         }
         proxy = new WebProxy(Ok_ip_port_ar[0], int.Parse(Ok_ip_port_ar[1]));
       }
       req.Proxy = proxy;
       req.CookieContainer = cookieJar1;
       res = (HttpWebResponse)req.GetResponse();

       Stream = res.GetResponseStream();
       reader = new StreamReader(Stream);
       reader_str = reader.ReadToEnd();

       htmlDoc = new HtmlAgilityPack.HtmlDocument();
       htmlDoc.LoadHtml(reader_str);

       var images = from image in htmlDoc.DocumentNode.Descendants("img")
                             select image;

       string address = string.Empty;

       foreach (var image in images)
       {
          if (image.Attributes["src"] != null)
          {
             string[] address_ar = image.Attributes["src"].Value.Split('/');
             string address_ar_last = address_ar[address_ar.Length - 1];
             char[] address_ar_last_char = address_ar_last.ToCharArray();

             if (address_ar_last_char.Length == 8
                            &&
                            Char.IsUpper(address_ar_last_char[0])
                            &&
                            Char.IsUpper(address_ar_last_char[1])
                            &&
                            Char.IsUpper(address_ar_last_char[2])
                            &&
                            Char.IsUpper(address_ar_last_char[3]))
             {
                address = image.Attributes["src"].Value;
                string localFilename = @"c:\images-from-istgah\" + address_ar_last;

                using (WebClient client = new WebClient())
                {
                   client.DownloadFile(address, localFilename);
                }
             }
           }
         }
      }

      catch (Exception ex)
      {

      }
         reader.Close();
         Stream.Close();
         res.Close();
 }
Community
  • 1
  • 1
SilverLight
  • 19,668
  • 65
  • 192
  • 300
  • Would appreciate a condensed code sample to specify the problem. – David S. Mar 06 '13 at 18:03
  • so sorry man, i removed junk codes... – SilverLight Mar 06 '13 at 18:08
  • 1
    What is exactly the problem? You talk about another thread without explaining your real issue. If you want to retry X times before giving up and continuing with another line, do the same as the other thread does. Add an index with a max number of retry inside a do...loop. – Léon Pelletier Mar 06 '13 at 18:20

1 Answers1

2

I don't think that you can with a foreach. It is designed to give you the next item in the iterator.

If I were you I would use an ordinary for-loop with an iterator. That way you can control when to go to the next item.

Edit:

When writing it, a while actually made more sense in C#.

IEnumerator<String> iter = list.GetEnumerator();
bool bHasMore = iter.MoveNext();
while (bHasMore) {
  try {
    ...
    bHasMore = Iter.MoveNext();
  }
  catch ...
}

I dont have the entire refernce in my head, so you might need to look something up to get it to compile, but I hope not.

fredrik
  • 6,483
  • 3
  • 35
  • 45