Results 1 to 7 of 7

Thread: Get status code with HttpClient

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Get status code with HttpClient

    I am using the following code to get a string from a server:

    Code:
    Dim handler = New HttpClientHandler With {.UseCookies = False}
    Dim http As New HttpClient(handler)
    Dim url1 As String = "SomeUrlString"
    
    http.DefaultRequestHeaders.Clear()
    http.DefaultRequestHeaders.Add("User-Agent", useragent)
    http.DefaultRequestHeaders.Add("Cookie", "mac=" & myMac)
    
    Try
    
        result = http.GetStringAsync(url1).Result
    
    Catch ex As Exception
        Console.WriteLine(ex.ToString)
    End Try
    I would like to be able to get the status code that is returned by the server. ie. 200, 404, etc., but I can't figure out how to do it.
    Can anyone help me out? Thanks...

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,958

    Re: Get status code with HttpClient

    Quote Originally Posted by nbrege View Post
    I am using the following code to get a string from a server:

    Code:
    Dim handler = New HttpClientHandler With {.UseCookies = False}
    Dim http As New HttpClient(handler)
    Dim url1 As String = "SomeUrlString"
    
    http.DefaultRequestHeaders.Clear()
    http.DefaultRequestHeaders.Add("User-Agent", useragent)
    http.DefaultRequestHeaders.Add("Cookie", "mac=" & myMac)
    
    Try
    
        result = http.GetStringAsync(url1).Result
    
    Catch ex As Exception
        Console.WriteLine(ex.ToString)
    End Try
    I would like to be able to get the status code that is returned by the server. ie. 200, 404, etc., but I can't figure out how to do it.
    Can anyone help me out? Thanks...
    Don't just catch Exception instead catch https://learn.microsoft.com/en-us/do...n?view=net-8.0 as this will give you the status code if an error occurs. If there was no error then it is probably safe to assume the status code was 200.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Get status code with HttpClient

    PD ... I did try HttpRequestException, but it never gets thrown. The only ones that get thrown are generic Exception, as in my code above, or AggregateException. BTW, the code above is running in a thread, if that makes any difference.

  4. #4
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,958

    Re: Get status code with HttpClient

    Quote Originally Posted by nbrege View Post
    PD ... I did try HttpRequestException, but it never gets thrown. The only ones that get thrown are generic Exception, as in my code above, or AggregateException. BTW, the code above is running in a thread, if that makes any difference.
    Typically AggregateException is thrown by code executing as part of the Task Parallel Library or PLinq, if you are getting an AggregateException then you probably want to investigate the exception's InnerException property to get access to the real exceptions.

    I would be surprised if HttpClient is throwing a generic Exception though - it should only throw exception's derived from Exception.

  5. #5
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Get status code with HttpClient

    If change the expected string to HttpResponseMessage , so getAsync then you can use.
    response.EnsureSuccessStatusCode();
    After the getasync
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Get status code with HttpClient

    Quote Originally Posted by sapator View Post
    If change the expected string to HttpResponseMessage , so getAsync then you can use.
    response.EnsureSuccessStatusCode();
    After the getasync
    Can you give me a quick example?

  7. #7
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Get status code with HttpClient

    I forgot and was writing in C# but:

    Code:
      using (HttpClient client = new HttpClient())
                {
                    HttpResponseMessage response = await client.SendAsync(request1);
                    
                    try
                    {
                        response.EnsureSuccessStatusCode();
                    }
                   catch (Exception ex)
                    {
    
                        //ex.Message
                 //       return;
                   }
                   
                    responseBody1 = await response.Content.ReadAsStringAsync();
                }
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width