title: How to handle the 401 Error response using C#?
body: <p>I have a <code>API</code> which returns the <code>json response</code>.
  When I call the <code>API</code> from <code>Fiddler</code> it gives me the <code>json reponse</code>
  as shown below:</p>
  
  <p><a href="https://i.stack.imgur.com/F0u84.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/F0u84.png" alt="enter image description here"></a></p>
  
  <p><strong>JSON Response:</strong>
  <a href="https://i.stack.imgur.com/iiS1X.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iiS1X.png" alt="enter image description here"></a></p>
  
  <p><strong>Call to API from Web page:</strong></p>
  
  <pre><code>protected void FinalCall()
      {
  
         // try
         // {
              string url = txtdomainURL.Text.Trim(); 
              string apiname = txtAPIname.Text.Trim();
              string apiURL = apiname+"/"+txtStoreID.Text.Trim()+"/"+txtSerialNo.Text.Trim(); //"duediligence/1/45523354232323424";// 990000552672620";//45523354232323424";
              //duediligence/1/45523354232323424 HTTP/1.1
              string storeID = txtStoreID.Text.Trim();//Test Store ID:1 Live Store ID: 2
              string partnerID = txtPartnerID.Text.Trim();// "1";
              string scretKey = txtSecretKey.Text.Trim();// "234623ger787qws3423";
              string requestBody = txtRequestBody.Text.Trim(); //"{\"category\": 8}";
              string data = scretKey + requestBody;
  
  
              string signatureHash = SHA1HashStringForUTF8String(data);
              lblSignatureHash.Text = signatureHash;
              String userName = partnerID;
              String passWord = signatureHash;
              string credentials = userName + ":" + passWord;//Convert.ToBase64String(Encoding.ASCII.GetBytes(userName + ":" + passWord));
              var dataString = JsonConvert.SerializeObject(requestBody); //JsonConvert.SerializeObject(requestBody);
              var bytes = Encoding.Default.GetBytes(dataString);
  
  
              WebClient client = new WebClient();
              string base64 = Convert.ToBase64String(Encoding.ASCII.GetBytes(credentials));
              client.Headers[HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(credentials));
              lblBase64.Text = base64;
  
              client.Headers.Add(HttpRequestHeader.Accept, "application/json");
              client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
              //string response = cli.UploadString(url + apiURL, dataString); //"{some:\"json data\"}"
              string completeURLRequest = url + apiURL;
  
              //I GET ERROR HERE 
              var result = client.DownloadString(completeURLRequest);
              //CODE below this line is not executed
  
              //Context.Response.TrySkipIisCustomErrors = true;
              var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
              var jsonObject = serializer.DeserializeObject(result.ToString());
              //var result1 = client.UploadData(completeURLRequest, "POST", bytes);
              Response.Write(result);
              txtwebresponse.Text = jsonObject.ToString();
          //}
  </code></pre>
  
  <p>Now, When the same is executed from a web page it throws <code>exeception</code> <code>'401 Unauthorized Exception'</code>. So, instead of showing error page I want to read the returned <code>JSON</code> error response (as in fiddler) and show to user.</p>
  
  <p>Help Appreciated!</p>