以太网屏蔽无法连接服务器与保持连接

时间:2015-10-02 15:23:22

标签: arduino http-headers connection ethernet


我想使用以太网屏蔽从我的Apche服务器(localhost)发送/获取数据。

Ethernet(or Esp8266) ======(request.php by post method)==>>> server 
Ethernet(or Esp8266) <<<=====(respond header and data)==== server

我向服务器发送标头以获取数据。

在我的标题中:
如果我使用 连接:关闭 。这种方式似乎没问题。
但是当我使用 连接时:保持活着 。它无法连接或获取任何数据直到连接超时 这是我的标题以及将数据发送到服务器的方式:

String httpRequest(String data){
   String result="";
   if(client.connect(server, 80)){
    Serial.println("connected");

    client.println("POST /pihome1/getStatus.php HTTP/1.1");
    client.println("Host: localhost");
    client.println("Accept: *" "/" "*");
    client.println("Content-Length: " + String(data.length()));
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.println("Connection: keep-alive");
    client.println();
    client.print(data);
  }
  else {
    //if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
//Reading data
    unsigned long start = millis();
    int timeout = 2000;
    while (millis() - start < timeout){ 
      while (client.available()) {
        char c = client.read();
        result += c;
      }  
    }
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.flush();
    client.stop();
  }
   return result;
}

第一次成功后,直到超时连接才能连接。

========Request to server=========
connected
HTTP/1.1 200 OK
Date: Sun, 04 Oct 2015 08:03:46 GMT
Server: Apache/2.4.4 (Win32) OpenSSL/0.9.8y PHP/5.4.16
X-Powered-By: PHP/5.4.16
Content-Length: 10
Keep-Alive: timeout=50, max=100
Connection: Keep-Alive
Content-Type: text/html



1&1eos
========End of request=========
========Request to server=========
connection failed

========End of request========
请告诉我有什么问题?

0 个答案:

没有答案