Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a lab exercise which is given in the MCTS 70-536.
I am failed to understand that what is "urls" in foreach loop, whether it is an object or......

C#
using System;
using System.Net;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (string url in urls)
                ThreadPool.QueueUserWorkItem(GetPage, url);
        }
        static void GetPage(object data)
        {
            string url = (string)data;

            WebResponse wr = WebRequest.Create(url).GetResponse();

            Console.WriteLine(url + ": " + wr.Headers["Content-Length"]);

            wr.Close();
        }
    }
}
Posted

1 solution

Hmm. There seems to be some code missing here. Basically, urls would probably be a list of strings (or array of them), which contains each individual url that needs to be passed into the GetPage method as the data object.

To test it, before the foreach, add the following:
C#
List<string> urls = new List<string>();
urls.Add("http://www.codeproject.com/");
urls.Add("http://www.google.com/");
 
Share this answer
 
v2
Comments
Parampreet.CCIT 19-Mar-12 7:00am    
thank u very much sir for ur support, now this program works properly
but i still have a question that what is Content-length in it, what is the function of wr.Headers in it???
The output of the program only shows the content-length of codeproject.com not for google or any other site, why?
Pete O'Hanlon 19-Mar-12 7:56am    
The issue you are facing here is that possibly the code will run to completion before it has a chance to retrieve the other sites. This is the whole nature of a threaded environment - things happen independently on background threads, and don't block the main thread from completing.
Parampreet.CCIT 27-Mar-12 7:02am    
I can't getting your answer...
Parampreet.CCIT 27-Mar-12 7:07am    
this program shows only the Content-length of codeproject.com not for any other site

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900