我最终想要向我的Amazon EC2服务器发送GET请求,但是现在我只需要以某种方式获得GET请求 . 为此,我正在尝试向Google发送GET请求作为测试 .

我正在使用带有以太网屏蔽的Arduino Uno通过DHCP连接到互联网 .

我的问题是虽然client.connect()似乎工作,但client.available无法使用谷歌或我的EC2服务器 . 但是,如果我的问题出在其他地方,请告诉我 .

我可以ping谷歌并使用以下方法进行Telnet GET请求模拟:-telnet www.google.com 80 -GET / search?q = arduino HTTP / 1.0

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[] = { 0x90, 0xA2, 0xDA, 0x0F, 0xD2, 0xAF };

//byte mac[] =   {0xf0, 0x1f, 0xaf, 0x33, 0x62, 0x2f };

IPAddress ip(192,168,1,11);

// initialize the library instance:
EthernetClient client;

//char server[] = "ec2-54-69-168-77.us-west-2.compute.amazonaws.com";
char server[] = "www.google.com";

double dummyValue = 7.5;

void setup()
{
  Serial.begin(9600);

  // attempt a DHCP connection:
  Serial.println("Attempting to get an IP address using DHCP:");
  if (!Ethernet.begin(mac)) {
    // if DHCP fails, start with a hard-coded address:
    Serial.println("failed to get an IP address using DHCP, trying manually");
    Ethernet.begin(mac, ip);
  }

  Serial.print("My address:");
  Serial.println(Ethernet.localIP());
}

void loop()
{
    // connect to the server
    for(int i = 0;i <100 ; i++) {  
    if (client.connect(server, 80)) {
        // print to serial monitor
        Serial.println("connected...");
        Serial.println("ARDUINO: forming HTTP request message");

        // send data the server through GET request
        //client.print("GET /~sclaybon3/firstdatatest.php?reading=3 HTTP/1.0\r\n");
        client.print("GET /search?q=arduino HTTP/1.0\r\n");
        Serial.println("Get request");
        //client.print(dummyValue);
        //client.print(" HTTP/1.1");
        //client.print("Host: ec2-54-69-168-77.us-west-2.compute.amazonaws.com\r\n");
        client.print("Host: www.google.com\r\n");
        //Serial.println("Host:www.google.com");
        //client.print("Connection: close\r\n\r\n");
        client.print("\r\n");


        Serial.println("ARDUINO: HTTP message sent");        

        // give server some time to receive and store data
        // before asking for response from it
        delay(1000);

        // get the response from the page and print it to serial port
        // to ascertain that data was received properly
        if(client.available())
        {
            Serial.println("ARDUINO: HTTP message received");
            Serial.println("ARDUINO: printing received headers and script response...\n");

            while(client.available())
            {
                char c = client.read();
                Serial.print(c);
            }
        }
        else
        {
            Serial.println("ARDUINO: no response received / no response received in time");
        }

        client.stop();
    }
    } 

    // do nothing forever after:
    while(true);
}

Arduino输出:

Attempting to get an IP address using DHCP:
My address:192.168.1.11
connected...
ARDUINO: forming HTTP request message
Get request
ARDUINO: HTTP message sent
ARDUINO: no response received / no response received in time