Search code examples
c#.netasynccallbackget-request

How to call AsyncCallback with a parameter in an BeginGetRequestStream (C#)


    public void SendPost(string code)
    {
        // Create the web request object
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Resource.Url);
        webRequest.Method = "POST";
        webRequest.ContentType = "application/x-www-form-urlencoded";

        //Start the request
        webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest);
    }

I want to call the GetRequestStreamCallback with an parameter.

Does anyone know how to do this?


Solution

  • Use a lambda instead of a method group.

    That is:

    webRequest.BeginGetRequestStream(new AsyncCallback(result => GetRequestStreamCallback(result, someParameter)), webRequest);