Arduino以太网HTTP请求到无端口80

时间:2013-07-01 21:09:57

标签: http arduino ethernet raw-ethernet

我有一个REST接口,可以侦听端口8733.我可以使用Fiddler对其进行测试,然后回答。

我已经复制了Fiddler请求并使用以下程序将其放入Arduino中。 当我运行它时,我的REST界面没有被击中。我试图从另一台有效的PC发送请求和Fiddler。所以没有防火墙问题。我也尝试向Arduino发送消息,它收到它,因此没有连接问题。

如何解决此问题?

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

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,177);
IPAddress serverIP(192,168,1,39);
String serverIPString = "192.168.1.39";
int serverPort = 8733;
String ChargepointId = "";

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
boolean isProcessingCommand = false;
boolean isAvailableForCharging = false;
boolean isConnected = false;
String currentCommand = "";
String lastJsonMessage = "";

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


    // Start the Ethernet connection and the server:
    Ethernet.begin(mac, ip);
    server.begin();
    Serial.print("server is at ");
    Serial.println(Ethernet.localIP());
}

void loop() {
    EthernetClient client;
    // If there's a successful connection:
    Serial.println("connecting...");
    if (client.connect(serverIP, 8733)) {
        if (client.available()) {
            client.println("PUT http://192.168.1.39:8733/Test_Time_Addresses/WebAPI.Services/ChargepointExternal/AcceptedConnection HTTP/1.1");
            client.println("Content-Type: application/json; charset=utf-8");
            client.println("Host: 192.168.1.39:8733");
            client.println("Content-Length: 56");
            client.println("Expect: 100-continue");
            client.println("Connection: Keep-Alive");
            client.println("");
            client.println("{\"ChargepointId\":\"e6bd0980-4c5b-4f76-955c-02a8269f44a9\"}");
            client.println("");
            delay(1000);
            Serial.println("DONE");
        }
    }
    else {
      // If you couldn't make a connection:
      Serial.println("connection failed");
      Serial.println();
      Serial.println("disconnecting.");
      client.stop();
    }
}

1 个答案:

答案 0 :(得分:1)

这条线错了:

if (client.available()) {

非常感谢,抱歉。