How to check error type in C#

Anurag Sharma

Total Post:40

Points:280

Posted by  Anurag Sharma
C#  Error 
 1734  View(s)
Ratings:
Rate this:

Hi Guys

I am making an external API call in my system. If the user is not logged in code will generate a 404 error

And my Code is like this

 

    public string  ExecuteGetRequest()
    {
        try
        {
            _url = Qparams != null ? Utility.WEBAPI_ENDPOINT  + Type + "?" + Qparams : Utility.WEBAPI_ENDPOINT + Type; ;
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(_url);
 
            if (!string.IsNullOrEmpty(SessionValues.UserID)&& !string.IsNullOrEmpty(SessionValues.AccessToken))
            {
                var handler = new HttpClientHandler();
                handler.Credentials = new NetworkCredential(SessionValues.UserID, SessionValues.AccessToken);
                string authInfo = SessionValues.UserID + ":" + SessionValues.AccessToken;
                authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
                string username = SessionValues.UserID;
                String password = SessionValues.AccessToken;
                String encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(username + ":" + password));
                httpWebRequest.Headers.Add("Authorization", "Basic " + encoded);
 
 
            }
            httpWebRequest.Method = "GET";
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
 
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream(), Encoding.UTF8))
            {
                _result = streamReader.ReadToEnd();
            }
        }
        catch (Exception ex)
        {
          Log.Add(LogTypes.Error,100, ex.ToString());
        }
        return _result;
    }

 

Thanks

Answer

Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.