Using code from StackOverflow:

HttpWebRequest webRequest;

void StartWebRequest()
{
    webRequest.BeginGetResponse(new AsyncCallback(FinishWebRequest), null);
}

void FinishWebRequest(IAsyncResult result)
{
    webRequest.EndGetResponse(result);
}

How do you access the response to the request?

Dani AI

Generated

Nice catch, . A few practical follow-ups that people running into the same "what now?" question will find useful.

If you stick with the APM pattern (Begin/End), pass the request as the AsyncState and do the real work inside the callback. Always wrap the response and its stream in using blocks and handle WebException so you can read any error response the server returned:

request.BeginGetResponse(ar =>
{
    var req = (HttpWebRequest)ar.AsyncState;
    try
    {
        using (var resp = (HttpWebResponse)req.EndGetResponse(ar))
        using (var sr = new StreamReader(resp.GetResponseStream()))
        {
            string body = sr.ReadToEnd();
            // handle body
        }
    }
    catch (WebException ex)
    {
        var err = ex.Response as HttpWebResponse;
        // read error body from err?.GetResponseStream()
    }
}, request);

For new code prefer the Task-based API (HttpClient + async/await). It is simpler, less error-prone, and supports cancellation and modern patterns:

static readonly HttpClient client = new HttpClient();

async Task<string> FetchAsync(string url, CancellationToken ct = default)
{
    using (var res = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, ct))
    {
        res.EnsureSuccessStatusCode();
        return await res.Content.ReadAsStringAsync();
    }
}

Additional practical tips:

  • Reuse HttpClient (don’t create one per request) or use IHttpClientFactory in .NET Core to avoid socket exhaustion.
  • If you expect error payloads, read ex.Response (in APM) or examine HttpResponseMessage in HttpClient to log/parse error details.
  • For large downloads, read the response stream in chunks or use ResponseHeadersRead to avoid buffering the whole body.
  • If running on a UI thread, marshal results back to that thread before updating UI components.

These points make the next steps after getting a response safe and reliable.

I realise I have asked the dumbest question ever, just dawned on me the EndGetResponse returns a HttpWebResponse object.... It's early I tell you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.