C
C#15mo ago
Aeon Vex

❔ Catching an error status code in HTML agility pack when using LoadFromWebAsync

I'm using HTML agility pack to request multiple web pages simultaneously. I'm struggling to see how I can safely do error handling when using LoadFromWebAsync() however, given that the returned result does not include any status code, and the only way to check the http response code is via a callback on the HtmlWeb instance or via a public property. Is there a way I can catch and handle http error codes in a thread safe way?
private HtmlWeb _htmlWeb = new HtmlWeb();

async Task<SomeData> GetDataAsync()
{
// _htmlWeb.PostResponse += ... ?
// Can the callback be used to handle status codes for these page loads?

var firstPageLoad = _htmlWeb.LoadFromWebAsync(firstUrl);
var secondPageLoad = _htmlWeb.LoadFromWebAsync(secondUrl);

await Task.WhenAll(firstPageLoad, secondPageLoad);

// can't check _htmlWeb.StatusCode here because another thread might have already modified it, or whichever page loaded first might have gotten an error code, which was then overwritten with 200 OK by the second page load

// if either returned anything but 200 OK, handle it or throw an exception...

var firstPage = await firstPageLoad;
var secondPage = await secondPageLoad;

// etc ...
}
private HtmlWeb _htmlWeb = new HtmlWeb();

async Task<SomeData> GetDataAsync()
{
// _htmlWeb.PostResponse += ... ?
// Can the callback be used to handle status codes for these page loads?

var firstPageLoad = _htmlWeb.LoadFromWebAsync(firstUrl);
var secondPageLoad = _htmlWeb.LoadFromWebAsync(secondUrl);

await Task.WhenAll(firstPageLoad, secondPageLoad);

// can't check _htmlWeb.StatusCode here because another thread might have already modified it, or whichever page loaded first might have gotten an error code, which was then overwritten with 200 OK by the second page load

// if either returned anything but 200 OK, handle it or throw an exception...

var firstPage = await firstPageLoad;
var secondPage = await secondPageLoad;

// etc ...
}
The only option I see would be to use the HtmlWeb.PostResponse callback, but I'm not sure how I could use that to cleanly handle error status codes in those specific tasks.
1 Reply
Accord
Accord15mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.