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 6736309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:06:53+00:00 2026-05-26T11:06:53+00:00

I am working on a async web request. and need to depends on the

  • 0

I am working on a async web request. and need to depends on the response to do a message return.

was thinking to do sth like following

// creating request

string messageToReturn = string.empty;
request.BeginGetResponse(ar => 
            { 
                HttpWebRequest req2 = (HttpWebRequest)ar.AsyncState; 
                var response = (HttpWebResponse)req2.EndGetResponse(ar);

                // is it safe to do this?
                messageToReturn = "base on respone, assign different message"; 


            }, request);

// will i get any response message? i will always get empty right?
// since response is handle in another thread
return messageToReturn;

what is the best way to do that?

  • 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-26T11:06:54+00:00Added an answer on May 26, 2026 at 11:06 am

    You are right, that variable will always be empty because you fired off an asyncronous request with the BeginGetResponse method. So really you have a few options here. You can either block the executing thread until the response comes back (probably a really bad idea unless you have a very strong argument for doing this), or you could use an event based asynchronous pattern to alert callers when your response returns…

    Consider some of your code wrapped in a method

    public void GetMessageAsync()
    {
        string messageToReturn = string.empty;
        request.BeginGetResponse(ar => 
                { 
                    HttpWebRequest req2 = (HttpWebRequest)ar.AsyncState; 
                    var response = (HttpWebResponse)req2.EndGetResponse(ar);
    
                    // is it safe to do this?
                    messageToReturn = "base on respone, assign different message"; 
    
    
                }, request);
    }
    

    To wire up an event based pattern here. We define a custom EventArgs class and a custom event which callers can listen for and which we will fire when the response comes back.

    public class StringEventArgs : EventArgs
    {
       public string Message { get; set; }
    }
    
    public event EventHandler<StringEventArgs> MessageReturned;
    
    public void GetMessageAsync()
    {
        //string messageToReturn = string.empty;
        request.BeginGetResponse(ar => 
                { 
                    HttpWebRequest req2 = (HttpWebRequest)ar.AsyncState; 
                    var response = (HttpWebResponse)req2.EndGetResponse(ar);
    
                    //messageToReturn = "base on respone, assign different message"; 
                    this.MessageReturned(this, new StringEventArgs { Message = response.ToString() });
    
                }, request);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on creating an async handler for ASP.NET that will execute a slow
Now I'm working on a web project. I need to create a XML document
I have a web method with a signature like this: public string[] ToUpper(string[] values)
I am currently working on a complex web interface and backend, that will need
Working in Eclipse on a Dynamic Web Project (using Tomcat (v5.5) as the app
Working with TCL and I'd like to implement something like the Strategy Pattern .
I am working on a Web application that needs to send XML to a
I'm working on this Silverlight 3.0 Project that is completely filled with Async Calls
I am working on syncing two business objects between an iPhone and a Web
I'm working on a script to load some images async using jQuery. Here is

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.