Merhaba ,

Kaynak :

https://stackoverflow.com/questions/7261986/how-to-get-error-information-when-httpwebrequest-getresponse-fails 

HttpWebRequest nesnesi bazı http status kodlarına göre hata dönebilir böyle bir durumda c# kodunuz catch'e düşecektir. Bu aşamada catch bloğunda bu responseların body kısmından hata ile ilgili içeriği alabilirsiniz.

try
{

            WebRequest webRequest = WebRequest.Create(txtUrl.Text);
            HttpWebRequest httpRequest = (HttpWebRequest)webRequest;

            httpRequest.Method = "POST";
            httpRequest.ContentType = "application/json; charset=utf-8";

            string sAuthorization = "Authorization: Basic " + txtApiKey.Text;
            httpRequest.Headers.Add(sAuthorization);

            httpRequest.ProtocolVersion = HttpVersion.Version11;
            httpRequest.Accept = "text/xml";
            httpRequest.Credentials = CredentialCache.DefaultCredentials;
            if (txtHeader.Text != "")
            {
                httpRequest.Headers.Add(txtHeader.Text);
            }

            Stream requestStream = httpRequest.GetRequestStream();
            //Create Stream and Complete Request             
            StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.ASCII);
            streamWriter.Write(txtRequestData.Text);
            streamWriter.Close();
            
            HttpWebResponse wr = (HttpWebResponse)httpRequest.GetResponse();
            StreamReader srd = new StreamReader(wr.GetResponseStream());
            txtResponseData.Text = srd.ReadToEnd();



}
catch (WebException ex)
{
    using (var stream = ex.Response.GetResponseStream())
    using (var reader = new StreamReader(stream))
    {
        txtResponseData.Text = reader.ReadToEnd();
    }
}
catch (Exception ex)
{
    // Something more serious happened
    // like for example you don't have network access
    // we cannot talk about a server exception here as
    // the server probably was never reached
}

 

İyi çalışmalar