Session drops while receiving response from POST request

Hi,

I am trying to carry out a XML-RPC-like call from my Arduino Mega to a local server listening on port 8169.

The response I print on the Serial Monitor reads "HT" (first two letters of the "HTTP/1.1..." response) before connection breaks. When I use my code to call port 80, I receive a proper 404 response. When I do the PUT request to port 8169 from my computer using POSTMAN, I receive "200 OK". Any idea what the problem might be?

Here is my test code:

#include <SPI.h>
#include <Ethernet.h>


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(192,168,1,2); 
IPAddress ip(192, 168, 1, 119);

EthernetClient client;

String PostData = "<methodCall><methodName></methodName><params><param></param></params></methodCall>"; // payload goes here

void setup() {

  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect.
  }

  Ethernet.begin(mac, ip);
  
  delay(1000);
  Serial.println("connecting...");

  if (client.connect(server, 8169)) {
    Serial.println("connected");

    client.println("POST /xmlrpc/common HTTP/1.1");
    client.println("Host: 192.168.1.2:8169");
    client.println("User-Agent: Arduino/1.0");
    client.println("Connection: close");
    client.print("Content-Length: ");
    client.println(PostData.length());
    client.println();
    client.println(PostData);
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop() {
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    while (true);
  }
}

And here the monitor response again:

connecting...
connected
HT
disconnecting.

Thanks for your help!

that is odd as client connected() should evaluate to true whilst there is unread data. so perhaps it is timing out somehow. try debugging by inserting a delay after sending the data and perhaps another 50-100 ms within a while loop to go inside the if conditional.

if (client.available()) {
    while(client.available()){
     char c = client.read();
     Serial.print(c);
     delay(50);
    }
}

You should have two while loops

   while(client.connected())
   {
      while(client.available() > 0)
      {
         // read the data
      }
   }

@jpadie:

     char c = client.read();
     Serial.print(c);
     delay(50);

WTF is that delay() in there for?

Great! That made it work. Thanks a lot! :slight_smile: