0

I was wondering if there's any way to ignore this 404 response from a server, because I'm importing a whole list of itemnumbers, and I don't want the whole process to stop just by 1 little 404 response.

Code:

string url = @"http://api.com/art,242;art,123;art,523";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
    html = reader.ReadToEnd();
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Bryan
  • 3,421
  • 8
  • 37
  • 77
  • @mjwills: I have tried do add a empty catch block, but when I do that, the debugger jumps to the empty catch block. I don't want to do that. – Bryan Aug 29 '18 at 10:14
  • When it goes to the catch block, press F5 to keep debugging. – mjwills Aug 29 '18 at 10:14
  • 1
    You could check the [status code](https://learn.microsoft.com/en-us/dotnet/api/system.net.httpwebresponse.statuscode?view=netframework-4.7.2#System_Net_HttpWebResponse_StatusCode) of the `response` instance and then conditionally proceed *if* the value is 200/OK. Or is an exception thrown by the `GetResponse()` call? – Igor Aug 29 '18 at 10:19
  • @Igor: That sounds good. And how do I proceed the execution of the code if an 404 is hit? – Bryan Aug 29 '18 at 10:24
  • You don't, 404 means not found so you would not execute the rest of *shown* code (reading the response). – Igor Aug 29 '18 at 10:25
  • @Igor: So lets say that I have the following call to the api that I'm working against: string url = @"http://api.com/art,242;art,123;art,523;222;art,333"; and let's say that I get a 404 on the itemnumber 123. What will happen to the rest after the itemnumber 123? – Bryan Aug 29 '18 at 10:27
  • 1
    It depends on your server. *Usually* your whole request succeeds or it fails. As to what your server does I do not know, ask the programmer or consult the documentation for what the expected result is should one of the passed in values be incorrect or not found. – Igor Aug 29 '18 at 10:29

0 Answers0