[SOLVED] connecting... connected disconnecting Ethernet2 Web Client

Hey everyone;

I’m new to forum, first of all i appreciate all these people who put effort to answer peoples questions, obviously arduino community is very good.

So i am supposed to write a code that communicates with certain web server. Objection is to prepare Arduino for it to get certain tasks from web server periodically.

I used the basic ethernet2 Web Client example; corrected the Ip and Mac adresses. The problem is client.available() and client.connected() returns 0 although it connects at first. Here is the code;

#include <SPI.h>
#include <Ethernet2.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {0x90, 0xA2, 0xDA, 0x10, 0x18, 0x7B};
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "http://airbotest.com";    // name address for Google (using DNS)
char path[] = "/GetTask_1002.aspx";

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(10, 11, 0, 2);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // try to congifure using IP address instead of DHCP:
    
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    
    client.print("GET ");
    client.print(path);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Connection: close");
    client.println();
    
    
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop() {
  // if there are incoming bytes available
  // from the server, read them and print them:
  delay(1);
  if (client.available()) {
    char c = client.read();
    Serial.println("c yi yazdırma denemesi");
    Serial.println(c);
  }
  
   
  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println("client connected");
    Serial.println(client.connected());
    Serial.println("client available");
    Serial.println(client.available());
    
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}

and the following is the outcome in the Serial monitor:

connecting…
connected
client connected
0
client available
0

disconnecting

If that library is compatible with the standard ethernet library included with the IDE, then you are not evaluating the return value from the connect function correctly. If you use a domain name rather than an IP, this is the correct evaluation. The only success return value is 1. If the DNS resolution fails, the function returns a negative number which evaluates to true.

// change this
  if (client.connect(server, 80)) {
// to this
  if (client.connect(server, 80) == 1) {

This is causing the DNS resolution to fail. Remove the protocol.

// change this
char server[] = "http://airbotest.com";
// to this
char server[] = "airbotest.com";