我正在尝试从 Arduino 向 Heroku 发出请求应用。它返回的只是 Heroku 的 404 页面。所以我用 Python 写了一个类似的脚本,它运行良好。
Arduino代码:
char serverName[] = "ruby-coffee-maker.herokuapp.com";
...
if (!client.connected()) {
Serial.println("connecting...");
if (client.connect(serverName, 80)) {
Serial.println("connected");
// Make a HTTP request
client.println("GET / HTTP/1.0");
client.println();
// Wait for response
delay(100);
// If data can be read from te server, print it
while (client.available()) {
char c = client.read();
Serial.print(c);
}
Serial.print("Done");
client.stop();
}
else {
// If you didn't get a connection to the server:
Serial.println("connection failed");
}
}
Python代码:
import httplib
h = httplib.HTTPConnection("ruby-coffee-maker.herokuapp.com")
h.request("GET", "/")
r = h.getresponse()
data = r.read()
print r.status, r.reason, "\"" + data + "\""
h.close()
我该如何解决这个问题?
最佳答案
您是否将 Arduino 代码发送的请求与 Python 代码进行了比较?根据我对HTTP/1.1的理解,你需要在GET请求中添加一个Host header filed。见 5.1.2 here .
抱歉,我刚刚看到您在 Arudino 请求中使用了 HTTP/1.0,是否有特殊原因?即使 1.0 不需要 Host-filed,您可能仍然需要包含它(例如,某些代理要求它存在)。
关于python - Arduino Heroku 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15879592/