forum

Home / DeveloperSection / Forums / How to check error type in C#

How to check error type in C#

Anurag Sharma240305-Dec-2014

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


Updated on 06-Dec-2014

Can you answer this question?


Answer

3 Answers

Liked By