Ethernet Shield: trouble sending more than one request

Hello everyone. I have an arduino Uno, with an ethernet shield. I'm trying to have it connect to a database(done), find its id(done), then periodically update its value(with a placeholder for now) depending on a sensor.
Currently it will fairly reliably send the initial "register me" request to the server, read the response containing its id, and attempt to send updates. Problem is, the updates will not send. They are pretty much the same as the initial request, and while it claims to be connected, there are no signs of any sort of message getting through. Client.connect returns true, and there doesn't seem to be a problem stopping the client (ran into that already, complete with workaround), and yet nothing gets through, even as the initial request works perfectly.

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

byte mac[] = { 0x8E, 0x8D, 0xBE, 0x8F, 0xFE, 0xED };
char server[] = "ec2-54-213-87-44.us-west-2.compute.amazonaws.com";
int myId = -1;
char myName[] = "PetersDesk";
int myVal = 23;

EthernetClient client;

void setup() {
  Serial.begin(9600);
  Serial.println("Setup Beginning");
  if(Ethernet.begin(mac)==0)
    Serial.println("Failed to configure Ethernet with DHCP");
  else 
    Serial.println("Phew");
  delay(1000);
  Serial.println("connecting...");
  
  while(!client.connect(server,4000)){
    Serial.println("Connection failed, trying again in 1s");
    delay(1000);
  }
  Serial.println("connected");
  client.println("POST /sensors/add HTTP/1.0");
  client.println("Content-Type: application/x-www-form-urlencoded");
  client.print("Content-Length: ");
  client.println((sizeof(myName)/sizeof(myName[0]))+4);
  client.println("Connection: close");
  client.println();
  client.print("city=");
  client.println(myName);
  client.println();
  Serial.println("All good");
  
  while (myId == -1) {
    if(client.available()){
      char c = client.read();
      Serial.print(c);
      
      if(c=='i'){
        getId();
      }
    }
  }
}

void loop()
{
  Serial.println("Connecting...");
  while (!client.connect(server, 4000)) {
    Serial.println("Connection failed, trying again in 1s");
    delay(1000);
  }
  if(client.connected())
    Serial.println("Connected");
  Serial.println(String("POST /sensors/")+ myId + String(" HTTP/1.0"));
  client.println(String("POST /sensors/")+ myId + String(" HTTP/1.0"));
  client.println("Content-Type: application/x-www-form-urlencoded");
  client.print("Content-Length: 6");
  client.println("Connection: close");
  client.println();
  client.println(String("val=")+myVal);
  client.println();
  Serial.println("Post apparently sent");
  stop();
  delay(1000);
}

void getId(){
  char c = client.read();
  Serial.print(c);
  if(c=='d'){
    c = client.read();
    Serial.print(c);
    if(c==':'){
      c = client.read();
      Serial.print(c);
      if(c >= '0' && c<= '9'){
        myId=c-48;
        c = client.read();
        while(c>='0' && c<='9'){
          myId*=10;
          myId+=(c-48);
          c = client.read();
        }
        Serial.print(String("\nMyId found: ") + myId);
        stop();
      }
    }
  }
}

void stop(){
  client.stop();
  while(client.connected()){
    client.stop();
    Serial.println("Stopping...");
  }
  Serial.println("Stopped");
}

Here's a typical output btw. The only POST that gets through is to /sensors/add

Setup Beginning
Setup Beginning
Phew
connecting...
connected
All good
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 4
Date: Wed, 24 Jul 2013 14:36:52 GMT
Connection: close

id:1
MyId found: 1Stopped
Connecting...
Connected
POST /sensors/1 HTTP/1.0
Post apparently sent
Stopped
Connecting...
Connected
POST /sensors/1 HTTP/1.0
Post apparently sent
Stopped
Connecting...

You should read all characters sent by the server and not just dropping the connection once you have what you wanted. In the loop request you don't even care about any response, as soon as the request is sent, you close the connection. Most web servers immediately stop the fulfillment of the request if the connection drops, so here might be your problem.

pylon:
You should read all characters sent by the server and not just dropping the connection once you have what you wanted. In the loop request you don't even care about any response, as soon as the request is sent, you close the connection. Most web servers immediately stop the fulfillment of the request if the connection drops, so here might be your problem.

Ok, I've modified my stop() method slightly to flush the client, as well as hopefully make it a little more robust. It still displays the same problem though, and I'm still just as confused

void stop(){
  
  while(client.connected()){
    Serial.println("Still connected");
    delay(200);
  }
  client.flush();
  if(!client.connected()){
    client.stop();
    Serial.println("Stopping...");
  }
}

If you want it robust, then use this. It also displays the response from the server. That might help.

void stop() {
// connectLoop controls the hardware fail timeout
  int connectLoop = 0;
  char inChar;

  while(client.connected()) {

    while(client.available()) {
      inChar = client.read();
      Serial.write(inChar);
      // set connectLoop to zero if a packet arrives
      connectLoop = 0;
    }

    connectLoop++;

    // if more than 10000 milliseconds since the last packet
    if(connectLoop > 10000) {
      // then close the connection from this end.
      Serial.println(F("\r\nTimeout"));
      client.stop();
    }
    // this is a delay for the connectLoop timing
    delay(1);
  }

  Serial.println(F("\r\ndisconnecting."));
  // close client end
  client.stop();
}

edit: Oops! Forgot to declare inChar.

Hah, it turned out the problem was on the end of the server, and it needed to send a reply. That was a load of fuss over nothing. The only problem now is the occasional massive delay before connecting

That is good, but my previous post still applies. You can settle for a few minutes to a couple hours before a fail, or go until the mains power fails long enough to kill the UPS your Arduino is using for power.