C#

How To Read API Exception In C#

In this article, we will learn about how we can read an API exception in C#.

The WebException class is thrown when an error occurs while accessing the API. It can check the Response property or the Status property of the exception to determine why the request failed and give a brief idea about the exception.

Syntax

try
{
    //API CALL
}
catch (WebException webex) { }

Example

Now, create a new project in ASP.NET Web Application and select the Web API framework when creating the project.

Open the HomeController.cs file and add the code in it.

using System.IO;
using System.Net;
using System.Web.Mvc;

namespace APIWebException.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Title = "Home Page";
            return View();
        }
        public string GetData()
        {
            string result = string.Empty;
            try
            {
                //Here, We are using wrong URL to generate an Exception.
                //The correct URL is : https://reqres.in/api/users/1
                string ParentURL = "https://reqres.in/users/1";
                var httpWebRequest = (HttpWebRequest)WebRequest.Create(ParentURL);
                httpWebRequest.ContentType = "application/json; charset=utf-8";
                httpWebRequest.Method = "GET";
                httpWebRequest.Accept = "*/*";
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    result = streamReader.ReadToEnd();
                }
            }
            catch (WebException webex)
            {
                WebResponse errResp = webex.Response;
                using (Stream respStream = errResp.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(respStream);
                    result = reader.ReadToEnd();
                }
            }
            return result;
        }
    }
}

The below part will read your exception in brief, so a user can identify why this exception actually occurs.

WebResponse errResp = webex.Response; 
using (Stream respStream = errResp.GetResponseStream()) { 
  StreamReader reader = new StreamReader(respStream);
  result = reader.ReadToEnd(); 
}

That’s it.

Output:

 

Faisal Pathan

Faisal Pathan is a founder of TheCodeHubs, .NET Project Manager/Team Leader, and C# Corner MVP. He has extensive experience with designing and developing enterprise-scale applications. He has good skills in ASP.NET C#, ASP.NET Core, ASP.NET MVC, AngularJS, Angular, React, NodeJS, Amazon S3, Web API, EPPlus, Amazon MWS, eBay Integration, SQL, Entity Framework, JavaScript, eCommerce Integration like Walmart, Tanga, Newegg, Group-on Store, etc. and Windows services.

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

2 years ago