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

  • Home
  • SEARCH
  • 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 8167937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:21:44+00:00 2026-06-06T20:21:44+00:00

Hi i am using HttpClient similar to this: public static Task<string> AsyncStringRequest(string url, string

  • 0

Hi i am using HttpClient similar to this:

public static Task<string> AsyncStringRequest(string url, string contentType)
{
    try
    {
        var client = new HttpClient();
        client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(contentType));

        return client.GetStringAsync(url).ContinueWith(task => {
            return task.Result;
        });
    }
    catch (AggregateException ex)
    {
        throw ex;
    }
    catch (WebException ex)
    {
        throw ex;
    }       
    catch (Exception ex)
    {
        throw ex;
    }
}

But i am having difficulties dealing with exceptions. I have added the additional catch blocks just to try and step throw, but none of the break points are caught in any of the catch blocks. I realise using Task the exception could occur on a different thread than the caller so the exception is wrapped in a aggregate container, but i am not sure what the best way to deal with these exceptions is.

For example i make a request to a web service and specific an invalid parameter in the request, and an exception is thrown. I want to me able to catch the aggregate exceptions and look at the innerexceptions to work out why the request has failed and return a friendly message.

So my question is, what is the best way to catch these aggregate exceptions and deal with them?

  • 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-06-06T20:21:47+00:00Added an answer on June 6, 2026 at 8:21 pm

    The exception is thrown by task.Result:

    var client = new HttpClient();
    client.DefaultRequestHeaders.Accept.Add(...);
    return client.GetStringAsync(url).ContinueWith(task =>
    {
        try
        {
            return task.Result;
        }
        catch (AggregateException ex)
        {
            throw ex;
        }
        catch (WebException ex)
        {
            throw ex;
        }       
        catch (Exception ex)
        {
            throw ex;
        }
    });
    

    Better: check if the task faulted before accessing task.Result:

    var client = new HttpClient();
    client.DefaultRequestHeaders.Accept.Add(...);
    return client.GetStringAsync(url).ContinueWith(task =>
    {
        if (task.IsFaulted)
        {
            var ex = task.Exception;
        }
        else if (task.IsCancelled)
        {
        }
        else
        {
            return task.Result;
        }
    });
    

    If you’re not actually doing something in the ContinueWith, you can simply omit it:

    var client = new HttpClient();
    client.DefaultRequestHeaders.Accept.Add(...);
    return client.GetStringAsync(url);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading a web page using HttpClient like this: httpclient = new DefaultHttpClient(); httpget
I'm trying to retrieve this page using Apache HttpClient: http://quick-dish.tablespoon.com/ Unfortunately, when I try
I'm using HttpClient 0.6.0 from NuGet. I have the following C# code: var client
I've been using httpClient to try to write a connection string to an https-base
I am sending post request from android to the url using httpclient like this
My current problem is very similar to this one . I have a downloadFile(URL)
I am using HttpClient 4.1.2 HttpGet httpget = new HttpGet(uri); HttpResponse response = httpClient.execute(httpget);
I have been using HTTPClient version 4.1.2 to try to access a REST over
I am using HttpClient to do Post request to a server URL. The server
i am try to call webservice using Android.I have used httpclient as well as

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.