C#.net Web Request could not get 404 custom error message while calling web API but postman does

2

I am calling external Web API in my own Web API using ServiceStack. For the unsubscribed user, external web API throws expected "404 Not found" exception with the custom message as "No subscriber with uuid: {...uuid sent}". I can see it in Postman enter image description here

but when I call it in my ServiceStack handler, I do not get the custom exception in exception details in the catch statement. Following is part of my code

    try{

           //Create Request
            var statusUri =new Uri(urlOfExternalWebAPI, UriKind.Absolute);
            var serviceRequest = (HttpWebRequest) WebRequest.Create(statusUri);
            serviceRequest.Method = "GET";
            serviceRequest.Headers.Add("Authorization",OauthToken.TokenType + " " + OauthToken.AccessToken);
            serviceRequest.Accept = "application/json";

            //Get the response.
            using (var response = serviceRequest.GetResponse())
            {                    
                using (var responseStream = response.GetResponseStream())
                {
                    if (responseStream != null)
                    {
                        using (var reader = new StreamReader(responseStream))
                        {
                            var responseFromServer = reader.ReadToEnd();
                        }                                
                    }
                }
            }
        }
        catch (WebServiceException e) // 
        {
            //Exception is not getting caught here 
            throw;
        }
        catch (Exception ex)
        {
            // exception is caught here but could not get the custom error message
            throw;
        }

Exception details:

System.Net.WebException
  HResult=0x80131509
  Message=The remote server returned an error: (404) Not Found.
  Source=System
  StackTrace:
   at System.Net.HttpWebRequest.GetResponse()
   at ...Host.Request.testRequestHandler.Get(GetStatus request) 
   in ...\\testRequestHandler.cs:line 231
   at ServiceStack.Host.ServiceRunner`1.Execute(IRequest request, Object instance, TRequest requestDto)

But I am not getting the custom message that I am getting in Postman in any field of the exception.

I need the exact custom message to differentiate between 404 due to invalid URL and 404 due to invalid subscriber. Please help.

Thanks in Advance.

c#
asp.net-web-api
servicestack
webrequest
asked on Stack Overflow Dec 13, 2019 by amol

2 Answers

1

Looks like you need to catch WebException as opposed to WebServiceException

I don't think you should return a 404 for an invalid subscriber either unless its a clear requirement!

answered on Stack Overflow Dec 13, 2019 by GerryFC
1

As you're not using ServiceStack's Generic C# Clients your HTTP Request wont throw a WebServiceException.

You're instead using .NET's HttpWebRequest directly so it's only going to throw a WebException, but ServiceStack does also include HTTP Utils which provides helpers for accessing Exceptions thrown by .NET's HttpWebRequest, e.g:

try 
{
    //...
} 
catch (Exception ex) 
{
    var clientEx = ex.IsAny400();
    var notFound = ex.IsNotFound();

    HttpStatusCode? errorStatus = ex.GetStatus(); // HTTP Status Code
    string errorBody = ex.GetResponseBody();      // Error Response Body (if any)
    var errorResponse = (HttpWebResponse)ex.Response;
    var description = errorResponse.StatusDescription;
}
answered on Stack Overflow Dec 13, 2019 by mythz

User contributions licensed under CC BY-SA 3.0