简体   繁体   English

Arduino以太网屏蔽-对服务器的GET请求无响应

[英]Arduino Ethernet Shield - No response on GET request to server

I have a code for the Arduino Ethernet Shield that will send a GET request to a server and return a PHP echo statement. 我有一个用于Arduino以太网屏蔽的代码,它将GET请求发送到服务器并返回PHP echo语句。

However, most of the time it fails to connect to the server. 但是,大多数情况下它无法连接到服务器。

When it does connect, I keep getting 403 Forbidden error or it says bad header format for "Host:". 当它确实连接时,我不断收到403 Forbidden错误,或者说“ Host:”的标头格式错误。

I have checked every forum and all StackOverflow links related to the topic, but none of their solutions worked. 我已经检查了每个论坛以及与该主题相关的所有StackOverflow链接,但是它们的解决方案均无效。 My code is attached below. 我的代码附在下面。

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

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "kanishkaganguly.byethost5.com";
IPAddress ip(192,168,0,103);

EthernetClient client;

void setup() {
 Serial.begin(9600);
 while (!Serial) {
    ;
 }

 Ethernet.begin(mac, ip);

 delay(1000);
 Serial.println("connecting...");

 if (client.connect(server, 80)) {
    Serial.println("connected");
    client.println("GET /test.php HTTP/1.1");
    client.println("Host: www.arduino.cc");
    client.println("User-Agent: arduino-ethernet");
    //client.println("User-Agent: Mozilla/5.0"); 
    //This supposedly fixed 403 error for another user
    client.println("Connection: close");
    client.println();
 }else {
    Serial.println("connection failed");
 }
}

void loop(){
  // if there are incoming bytes available 
  // from the server, read them and print them:
   if (client.available()) {
     char c = client.read();    
     Serial.print(c);
   }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
     Serial.println();
     Serial.println("disconnecting.");
     client.stop();

     // do nothing forevermore:
     for(;;)     ;  
   }
 }

I figured out the problem. 我解决了这个问题。 The client.println() as a new line isn't working for some reason. 由于某种原因, client.println()作为新行不起作用。 So, here is the updated code 所以,这是更新的代码

client.print("GET /checkcontrol.php HTTP/1.1\r\n");
client.print("Host: shahz.webatu.com\r\n");
client.print("User-Agent: arduino-ethernet\r\n");
client.print("Connection: close\r\n\r\n");

The \\r\\n is the right way to go about adding a new line for the server to recognize. \\r\\n是添加新行以供服务器识别的正确方法。

The Host header specifies the hostname of the site you're connecting to. Host标头指定您要连接的站点的主机名。 In this case, you are trying to connect to the domain kanishkaganguly.byethost5.com , but your Host header is set to www.arduino.cc . 在这种情况下,您尝试连接到域kanishkaganguly.byethost5.com ,但是您的Host标头设置为www.arduino.cc Usually this is incorrect. 通常这是不正确的。 The Host header should match the domain, so both should be kanishkaganguly.byethost5.com . Host标头应与域匹配,因此两者都应为kanishkaganguly.byethost5.com

When a client connects to a domain, the client first resolves the domain name to an IP, then makes the connection to that IP. 当客户端连接到域时,客户端首先将域名解析为IP,然后建立与该IP的连接。 Because the connection is made to the IP, the server does not know what domain name was looked up by the client. 因为已建立到IP的连接,所以服务器不知道客户端查找了哪个域名。 Because IPs are a limited resource, many hosting providers host multiple domains on a single IP. 由于IP是一种有限的资源,因此许多托管服务提供商在一个IP上托管多个域。 Without the Host header, the server would not know which page to return to the client. 如果没有Host标头,服务器将不知道要返回到客户端的页面。

The println command sends the text followed by "\\r\\n" so instead of changing every println for print, you could have added CRLF to the close line. println命令发送文本,后跟“ \\ r \\ n”,因此您可以将CRLF添加到结束行,而不是更改每个用于打印的println。

Client.println("Connection: close\\r\\n") Client.println(“ Connection:close \\ r \\ n”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM