0

I am using the HttpClient PostAsync method by passing requestUri and content but get a very generic error message:

One or more errors occurred

Could you please guide me. Not sure what might be causing this. When I use the same requestUri and content in postman it works as expected.

var client = new HttpClient();
var content = new StringContent(authXML);
content.Headers.ContentType = new MediaTypeHeaderValue("application/xml");
client.BaseAddress = new Uri(authorizationUri);
var result = client.PostAsync(authorizationUri, content).Result; //Generic error message "One or more errors occurred"
if (response.IsSuccessStatusCode)
{
    // SUCCESS
    // Do Something
}
else
{
    // ERROR
    // Do Something
}
pbj
  • 663
  • 1
  • 8
  • 19
  • Can you at least get the HTTP status code of the response? – Rui Jarimba Oct 02 '18 at 20:10
  • 5
    Ultimately, you'll want to use `await`, but in the meantime, use `.GetAwaiter().GetResult()` instead of `.Result` to see the unwrapped exception. I'd only be guessing right now, but I'm wondering if your `baseUri` and `authorizationUri` are both absolute, which might cause an exception. – Kirk Larkin Oct 02 '18 at 20:13
  • `One or more errors occurred` you need to learn to read and interpret the whole exception. It tells you a lot about the error! – usr Oct 02 '18 at 20:35
  • Thanks all for your response. I have the detailed exception. Basically I am running into "System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel." – pbj Oct 02 '18 at 22:27

2 Answers2

1

The provided code sample does not appear to obtain a handle to response. I think you need to obtain the response and then apply it to the result, as follows:

string result;
var response = client.PostAsync(authorizationUri, content).Result;
response.EnsureSuccessStatusCode();
result = response.Content.ReadAsStringAsync().Result;
nycdanielp
  • 187
  • 1
  • 2
-1

You need to either await or Wait for the result.

EDIT: One or more errors occurred is the aggegrate exception from the task. You probably want the inner

var client = new HttpClient();
var content = new StringContent(authXML);
content.Headers.ContentType = new MediaTypeHeaderValue("application/xml");
client.BaseAddress = new Uri(authorizationUri);
var post = client.PostAsync(authorizationUri, content);
try {
      post.Wait();
  }
  catch (AggregateException e) {
     foreach (Exception ie in e.InnerExceptions)
        Console.WriteLine("{0}: {1}", ie.GetType().Name,
                          ie.Message);
  }

if (post.Result.IsSuccessStatusCode)
{
    // SUCCESS
    // Do Something
}
else
{
    // ERROR
    // Do Something
}
ryanthedev
  • 26
  • 3
  • Are you sure this _absolutely_ solves the problem? – maccettura Oct 02 '18 at 20:14
  • Yes. It solves the error "One or more errors occurred". Which is what the question asks. He verified it works in postman. The rest of the syntax looks good. Except the if statement is in reference to a non existing variable. – ryanthedev Oct 02 '18 at 20:17