我正在尝试使用Arduino Uno从Web服务器获取数据。我正在使用SIM800L模块通过GPRS连接。以下是我用来连接Web服务器的代码。
#include <gprs.h>
#include <SoftwareSerial.h>
char http_cmd[] = "GET canopussl.com/info.php HTTP/1.1\r\n\r\n";
char buffer[512];
GPRS gprs;
void setup() {
Serial.begin(9600);
while(!Serial);
Serial.println("GPRS - HTTP Connection Test...");
gprs.preInit();
while(0 != gprs.init()) {
delay(1000);
Serial.println("init error");
}
while(!gprs.join("dialogbb")) { //change "cmnet" to your own APN
Serial.println("Error joining GPRS network");
delay(2000);
}
// successful DHCP
Serial.print("IP Address is ");
Serial.println(gprs.getIPAddress());
Serial.println("Init success, connecting to canopussl.com ...");
if(0 == gprs.connectTCP("canopussl.com", 80)) {
Serial.println("Successfully connected to canopussl.com!");
}else{
Serial.println("connect error");
while(1);
}
Serial.println("waiting to fetch...");
if(0 == gprs.sendTCPData(http_cmd))
{
gprs.serialDebug();
}
gprs.closeTCP();
gprs.shutTCP();
Serial.println("close");
}
void loop() {
}
输出就是这个。
GPRS - HTTP Connection Test...
IP Address is 10.84.3.49
Init success, connecting to canopussl.com ...
Successfully connected to canopussl.com!
waiting to fetch...
HTTP/1.0 403 Forbidden
Cache-Control: no-cache
Connection: close
Content-Type: text/html
<html><body><h1>403 Forbidden</h1>
Request forbidden by administrative rules.
</body></html>
CLOSED
我如何修改HTTP标头以接收服务器的正常输出?
答案 0 :(得分:1)
您没有发送强制性Host
标题。
您的HTTP请求应如下所示:
char http_cmd[] = "GET /info.php HTTP/1.1\r\nHost: canopussl.com\r\n\r\n";