1

I have the following Code in Visual Studio

using System;
using System.Windows.Forms;
using System.Net.Http;
using System.Web;
using System.Net;
using System.IO;

namespace Xml_Trial
{
    public partial class Form1 : Form
    {
        public Form1()
        { 
           InitializeComponent();
        }

        private void LoadUrl_click(object sender, EventArgs e)
        {
            Xmlcheck(TxtUrl.Text);
        }

        private static async void Xmlcheck(string TxtUrl)
        {
            try
            {
                HttpClient client = new HttpClient() ; //httpclient
                var request = new HttpRequestMessage(HttpMethod.Get, TxtUrl);
                HttpResponseMessage response = await client.GetAsync(request.RequestUri);
                 if (response.StatusCode == HttpStatusCode.OK) 
                    {
                       // Console.WriteLine((int)response.StatusCode); More code here
                    }
                response.Dispose();
            } 
            catch (HttpRequestException e)
            {
               MessageBox.Show(e.Message);
            }
        }
    }

}

I have written the code this way to get the 200 status code Console.WriteLine((int)response.StatusCode) What Code is else posssible to get more out of the httpstatuscode description rather than just "OK" or "200"....

CrazyFirewall
  • 33
  • 2
  • 7

1 Answers1

1

I believe you're looking for the ReasonPhrase property, which is "the reason phrase which typically is sent by servers together with the status code."

For example:

Console.WriteLine($"{(int)response.StatusCode} {response.ReasonPhrase}");

A list of all valid status codes (including my favourite, 418) with their default reason phrases is here: HTTP response status codes

Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84
  • That worked well ..It gave me `200 OK` I guess the ReasonPhrase would only give that much.. the rest would be a custom message that could be added by me.. What about the "HttpWorkerRequest Class" could it help in some form.. – CrazyFirewall Jun 24 '20 at 13:53
  • Help with what? What more are you looking for? – Gabriel Luci Jun 24 '20 at 14:13
  • Any furthur response or message that could come from the Server.. would the `HttpWorkerRequest` be an alternative to the `ReasonPhrase` – CrazyFirewall Jun 24 '20 at 14:27
  • The [documentation for that](https://learn.microsoft.com/en-us/dotnet/api/system.web.httpworkerrequest) says "In most cases, your code will not deal with HttpWorkerRequest directly because request and response data are exposed through the HttpRequest and HttpResponse classes." So no. The only other data is the body of the response: `await response.Content.ReadAsStringAsync()` – Gabriel Luci Jun 24 '20 at 14:31