Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1064835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:43:34+00:00 2026-05-16T19:43:34+00:00

The code is not complete (It does not build), as I am not able

  • 0

The code is not complete (It does not build), as I am not able to get the “item” in the following line in the WriteResponseData().

HttpWebResponse WebResp = (HttpWebResponse)item.EndGetResponse(result);

I must be missing something. Please point out.

below is the complete code:-

static string[] imageUris =
        { "http://www.cricinfo.com/db/PICTURES/CMS/118200/118217.2.jpg",
        "http://www.cricinfo.com/db/PICTURES/CMS/117500/117599.2.jpg",
        "http://www.cricinfo.com/db/PICTURES/CMS/117500/117598.2.jpg",
        "http://www.cricinfo.com/db/PICTURES/CMS/106400/106412.2.jpg",
        "http://www.cricinfo.com/db/PICTURES/CMS/106400/106411.2.jpg",
        "http://www.cricinfo.com/db/PICTURES/CMS/106200/106275.2.jpg",
        "http://www.cricinfo.com/db/PICTURES/CMS/106300/106362.2.jpg"
        };

        static void Main(string[] args)
        {
            SendAsynchronousRequests();
            Console.WriteLine("All the files have been requested and retrieved...");
            Console.ReadLine();
        }



static void SendAsynchronousRequests()
        {
            WebRequest[] requests = InitializeWebRequests();
            foreach (var item in requests)
            {
                Console.WriteLine("Trying to retrieve :-" + item.RequestUri.OriginalString);
                item.BeginGetResponse
                (
                   WriteResponseData,
                );
             }

        }

    static WebRequest[] InitializeWebRequests()
    {
        WebRequest[] requests = new WebRequest[imageUris.Length];
        int temp = 0;
        foreach (var item in imageUris)
        {
            requests[temp] = WebRequest.Create(item);
            //requests[temp].Method = "GET";
            temp++;
        }
        return requests;
    }
    static void WriteResponseData(IAsyncResult result)
    {
        HttpWebResponse WebResp = (HttpWebResponse)item.EndGetResponse(result);
        Console.WriteLine("WebResp.StatusCode :-" + WebResp.StatusCode);
        Console.WriteLine("WebResp.Server :- " + WebResp.Server);
        Console.WriteLine("WebResp.ContentLength :- " + WebResp.ContentLength.ToString());
        Console.WriteLine(Environment.NewLine);

    }

EDIT

So, the modified code for the method (which would compile) would be following :-

WebRequest request = (WebRequest)result.AsyncState;
            if (result != null && result.IsCompleted)
            {
                HttpWebResponse WebResp = (HttpWebResponse)request.EndGetResponse(result);
                Console.WriteLine("WebResp.StatusCode :-" + WebResp.StatusCode);
                Console.WriteLine("WebResp.Server :- " + WebResp.Server);
                Console.WriteLine("WebResp.ContentLength :- " + WebResp.ContentLength.ToString());
                Console.WriteLine(Environment.NewLine);
            }

It gives me following run-time error:-

Object reference not set to an
instance of an object. at
WebRequestUsingAPM.Program.WriteResponseData(IAsyncResult
result) in
\WebRequestUsingAPM\Program.cs:line 73
at
System.Net.LazyAsyncResult.Complete(IntPtr
userToken) at
System.Net.ContextAwareResult.CompleteCallback(Object
state) at
System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback
callback, Object state, Boolean
ignoreSyncCtx) at
System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback
callback, Object state) at
System.Net.ContextAwareResult.Complete(IntPtr
userToken) at
System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object
result, IntPtr userToken) at
System.Net.HttpWebRequest.ProcessResponse()

EDIT :-
I get it. result.AsyncState returns null because I pass null when I am calling the BeginGetResponse(), the second argument is the AsyncState and I am passing null there. I should pass the current WebRequest object there and that would do.

So, instead of following :-

item.BeginGetResponse
                    (
                       WriteResponseData, null
                    );

I should pass the item:-

item.BeginGetResponse
                        (
                           WriteResponseData, item
                        );
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T19:43:35+00:00Added an answer on May 16, 2026 at 7:43 pm

    As you have said, this code would’t compile simply because WriteResponseData method will not have reference to item object. You can use pass your request object as the state while calling begin request. See this code example from MSDN.

    Edit : a simplified code template would be

       item.BeginGetResponse(WriteResponseData, item);
    

    And modify WriteResponseData as

    static void WriteResponseData(IAsyncResult result)
    {
        WebRequest request = (WebRequest)result.AsyncState;
        WebResponse response = request.EndGetResponse(result);
        ....
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 530k
  • Answers 530k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I found the problem -- I was caching the frequently-requested… May 16, 2026 at 11:41 pm
  • Editorial Team
    Editorial Team added an answer The Like button coded to show "Recommend" is 84px wide… May 16, 2026 at 11:41 pm
  • Editorial Team
    Editorial Team added an answer Reference: A reference is a plain "A knows B" relation.… May 16, 2026 at 11:41 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I created a Materialized view using the following code: CREATE MATERIALIZED VIEW M_USER_HIERARCHY BUILD
After feedback, complete rewrite of the question. I have the following mark up :
I have a jqgrid whose html is displayed in the following way. I build
the below code works perfectly in FF and CHROME but not in IE. Please
I want to get a shipping/billing address id from a just complete order out
I am having problems with cURL not being able to connect to a server
I've done a complete clean uninstall of XCode and deleted the prefs and deleted
I have a requirement to build an application that a company can use to
Does doing good enough software take anything from you being a programmer? Here are
While the C# spec does include a pre-processor and basic directives (#define, #if, etc),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.