Arduino ethercad 库获取请求

时间:2021-02-06 10:22:01

标签: http arduino get ethercard

我有一个带有 MOD-ECN28J60 的 Arduino。

我可以向我的网络服务器发送一个 get 响应,但我只收到响应的标头,而不是正文;当我用笔记本电脑加载它时,我的整个身体都没有问题。

我尝试了 post 和 get 请求,没有返回正文。

我的代码:

#include <EtherCard.h>
const char apihost[] PROGMEM = "10.0.0.34";
static byte mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };

byte Ethernet::buffer[900];
Stash stash;
static byte session;

static void notifyMyAndroid () {
    byte sd = stash.create();
    stash.save();
    int stash_size = stash.size();

// Compose the http POST request, taking the headers below and appending
// previously created stash in the sd holder.
Stash::prepare(PSTR("GET /api HTTP/1.1" "\r\n"
                  "Host: 10.0.0.34:8088" "\r\n"
                  "\r\n"
                  "$H"),
             sd);

    // send the packet - this also releases all stash buffers once done
    // Save the session ID so we can watch for it in the main loop.
    ether.hisport = 8088;
    session = ether.tcpSend();
  }

void setup () {
  Serial.begin(57600);
  Serial.println("\nStarting Notify My Android Example");

  // Change 'SS' to your Slave Select pin, if you arn't using the default pin
  if (ether.begin(sizeof Ethernet::buffer, mymac, SS) == 0)
    Serial.println(F("Failed to access Ethernet controller"));
  if (!ether.dhcpSetup())
    Serial.println(F("DHCP failed"));

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);
  ether.printIp("DNS: ", ether.dnsip);

  if (!ether.dnsLookup(apihost))
    Serial.println(F("DNS lookup failed for the apihost"));
  ether.printIp("SRV: ", ether.hisip);

  notifyMyAndroid();
}

void loop () {
  ether.packetLoop(ether.packetReceive());

  char* reply = ether.tcpReply(session);
  if (reply != 0) {
    Serial.println("Got a response!");
    Serial.println(reply);
  }
}

0 个答案:

没有答案