|
-
Mar 6th, 2024, 04:01 PM
#1
Thread Starter
Frenzied Member
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...
-
Mar 6th, 2024, 04:04 PM
#2
Re: Get status code with HttpClient
 Originally Posted by nbrege
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.
-
Mar 6th, 2024, 06:15 PM
#3
Thread Starter
Frenzied Member
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.
-
Mar 7th, 2024, 04:40 AM
#4
Re: Get status code with HttpClient
 Originally Posted by nbrege
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.
-
Mar 7th, 2024, 08:23 AM
#5
Re: Get status code with HttpClient
If change the expected string to HttpResponseMessage , so getAsync then you can use.
response.EnsureSuccessStatusCode();
After the getasync
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Mar 7th, 2024, 09:56 AM
#6
Thread Starter
Frenzied Member
Re: Get status code with HttpClient
 Originally Posted by sapator
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?
-
Mar 7th, 2024, 10:09 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|