Ethernet can't connect internet after 404 error

Hi everyone, I am using pico with W5500. I am using this library githublink. I am using http get and post requests. Everything works fine. I am using get request from this api api.example.com/api/9873214 . This one is not real api , it is example. This 9873214 is a variable so it can be change. When this variable change to empty string api.example.com/api/, http get request gives 404 error and than it gives

// The response from the server is invalid, is it definitely an HTTP
// server?
static const int HTTP_ERROR_INVALID_RESPONSE = -4;

After this error, ethernet can't connect client even if I use ,

if (statusCode == -4)
EthernetClient client;
EthernetHttpClient  httpClient(client, serverAddress, port);
uint16_t index = millis() % NUMBER_OF_MAC;
Ethernet.begin(mac[index], 1000);

So, how can ethernet connect internet after 404 error or status -4 error ?

This code doesn't make sense. I believe it's not your actual code but some code you might think we know what you were doing.

Post your complete code!

Why are you changing your MAC address? Does your switch stomach that?

Hi, I have 2500 line codes.

int port = 80;
EthernetClient client;
EthernetHttpClient  httpClient(client, serverAddress, port);

void setup() {
}

void loop() {
  // do some task with hardware parts , this loops triggered by ethernet requests
}
void setup1() {
  uint16_t index = millis() % NUMBER_OF_MAC;
  Ethernet.begin(mac[index], 1000);
}
void loop1 () {
  unsigned long currentMillisGet = millis();
  if (currentMillisGet - previousMillisGet >= 400)
  {
    previousMillisGet = currentMillisGet;
    if (machine1)
    {
      String get_path = "/example/example/" + variableFromRfid;
      httpClient.get(get_path);
      int statusCode = httpClient.responseStatusCode();
      if (statusCode == 200)
      {
        String response = httpClient.responseBody();
        Serial.println(response);

      }
      else if (statusCode == -2)
      {
        if (object == 0) {
          uint16_t index = millis() % NUMBER_OF_MAC;
          Ethernet.begin(mac[index], 1000);
          object ++;
        }
        else {
          object = 0;
        }
      }

    }
  }
}

I use this lines ;

uint16_t index = millis() % NUMBER_OF_MAC;
Ethernet.begin(mac[index], 1000);

Ethernet gives status code -2 error after indefinite time . I solved by adding these two after when ethernet gives -2 error again.

When ethernet gives -4 and 404, I though maybe I can solve it again by adding these lines

if (statusCode == -4)
EthernetClient client;
EthernetHttpClient  httpClient(client, serverAddress, port);
uint16_t index = millis() % NUMBER_OF_MAC;
Ethernet.begin(mac[index], 1000);

but It couldn't work.

String get_path = "/example/example/" + variableFromRfid;

If this variableFromRfid return empty, etherner gives 404 error and status code -4. After this error, ethernet can't connect internet.

I added variable From Rfid !="" but I still don't know how to connect ethernet to internet when something gives 404 error.

I realize, after 500,502,503,504 error, it gives status code -4 and ethernet can't use http get or post request but I can observe the orange light flashing on the w850io (it seems to be flashing when there is internet)

I might be find solution. I write something like this;

  else if (statusCode == 500 || statusCode == -4 || statusCode || 503 || statusCode == 504 || statusCode == -3)
      {
        httpClient.stop();
        delay(500);
        EthernetHttpClient httpClient(client, serverAddress, port);
        uint16_t index = millis() % NUMBER_OF_MAC;
        Ethernet.begin(mac[index], 1000);
      }

when it gives these errors, I am stopping http client and then starting again. I don't know if this is good solution but seems like work fine for now.

The posted code doesn't have 2500 lines. And it doesn't compile!

Ethernet? The Ethernet library doesn't give a status code in your code. In your code the HTTP client library returns some status codes but they are not originating in the Ethernet library.

Add it where? That code probably won't compile (no block delimiters).

What does "can't connect Internet" mean?

You didn't add that in the posted code.

Have you tried to call httpClient.stop() if you received a statusCode other than 200? The library you're (probably) using is quite stateful, so you have to reset the state if you don't follow the expected call order.

The posted code doesn't have 2500 lines. And it doesn't compile!

2500 line code is really mess. I wrote without using class and voids so its really messy. That's why I didn't post it here.

Ethernet? The Ethernet library doesn't give a status code in your code. In your code the HTTP client library returns some status codes but they are not originating in the Ethernet library.

Yes, I couldn't find the problem and thought the wrong place was the problem, sorry.

What does "can't connect Internet" mean?

As I said, I defined my problem wrong. I should say it doesn't connect server, I guess.

Have you tried to call httpClient.stop() if you received a statusCode other than 200? The library you're (probably) using is quite stateful, so you have to reset the state if you don't follow the expected call order.

 else if (statusCode != 200 || statusCode == -2 )
      {
        httpClient.stop();
        delay(500);
        EthernetHttpClient httpClient(client, serverAddress, port);
        uint16_t index = millis() % NUMBER_OF_MAC;
        Ethernet.begin(mac[index], 1000);
      }

I am using above the code and for now it is working without problem. Thank you.