2016-11-06 277 views
0

http://www.universalcard.byethost7.com是我的服务器。我在哪里保存index.php文件。代码如下:ESP8266发送GET请求到远程服务器

<?php 
if(isset($_GET['username']) && isset($_GET['pin']) && isset($_GET['cost'])) { 

$username = $_GET['username']; 
$pin = $_GET['pin']; 
$cost = $_GET['cost']; 
$filecontent = "Username is: ".$username." and PIN is: ".$pin." and cost is: ".$cost."\n"; 
$filestatus = file_put_contents('uc.txt',$filecontent,FILE_APPEND); 
if($filestatus != false) 
{ 
    echo "Data written to file.."; 
}else{ 
    echo "Ohh sorry.."; 
} 

} else { 
    echo "Something went wrong.."; 
} 
?> 

我想用Arduino IDE发送ESP8266的GET请求。 在这个GET请求中,我用一些值(数据类型是String)发送了3个变量'username','pin'和'cost'。这些值附加到文件“uc.txt”。因此,当我使用浏览器发送请求时,值将附加到文本文件。

但是,当我尝试使用ESP8266送它不附加

Arduino的代码如下

#include <ESP8266WiFi.h> 
#include <WiFiClientSecure.h> 

const char* ssid = "rainbow"; 
const char* password = "12345678"; 

const char* host = "universalcard.byethost7.com"; 
const int httpsPort = 443; 

// Use web browser to view and copy 
// SHA1 fingerprint of the certificate 
//const char* fingerprint = "CF 05 98 89 CA FF 8E D8 5E 5C E0 C2 E4 F7 E6 C3 C7 50 DD 5C"; 

void setup() { 
    Serial.begin(115200); 
    Serial.println(); 
    Serial.print("connecting to "); 
    Serial.println(ssid); 
    WiFi.begin(ssid, password); 
    while (WiFi.status() != WL_CONNECTED) { 
    delay(500); 
    Serial.print("."); 
    } 
    Serial.println(""); 
    Serial.println("WiFi connected"); 
    Serial.println("IP address: "); 
    Serial.println(WiFi.localIP()); 

    // Use WiFiClientSecure class to create TLS connection 
    WiFiClientSecure client; 
    Serial.print("connecting to "); 
    Serial.println(host); 
    if (!client.connect(host, httpsPort)) { 
    Serial.println("connection failed"); 
    return; 
    } 

    String url = "/index.php?username=2bv14is114&pin=5555&cost=1111"; 
    Serial.print("requesting URL: "); 
    Serial.println(url); 

    client.print(String("GET ") + url + " HTTP/1.1\r\n" + 
       "Host: " + host + "\r\n" + 
       "User-Agent: BuildFailureDetectorESP8266\r\n" + 
       "Connection: close\r\n\r\n"); 

    Serial.println("request sent"); 
    while (client.connected()) { 
    String line = client.readStringUntil('\n'); 
    if (line == "\r") { 
     Serial.println("headers received"); 
     break; 
    } 
    } 
    String line = client.readStringUntil('\n'); 
    if (line.startsWith("{\"state\":\"success\"")) { 
    Serial.println("esp8266/Arduino CI successfull!"); 
    } else { 
    Serial.println("esp8266/Arduino CI has failed"); 
    } 
    Serial.println("reply was:"); 
    Serial.println("=========="); 
    Serial.println(line); 
    Serial.println("=========="); 
    Serial.println("closing connection"); 
} 

void loop() { 
} 

,串行监视器的输出低于

enter image description here

+0

你的代码说'byethehost7.com',但你的评论说'byethehost5.com'你发送你的请求到错误的网站? – leetibbett

+0

是的,一个有问题的小错误应该是universalcard.byethost7.com。我现在将编辑问题。 –

回答

0

您的主机有一些保护措施(可能针对机器人),如果不存在,那么预计会有一个由JavaScript设置的_test Cookie。

您可以通过首先使用浏览器访问该网站并将该cookie粘贴到代码中来获取cookie。
您需要从相同的IP执行此操作,以便将ESP8266引入服务器,因为Cookie是IP绑定的。
在这种情况下,如果您拥有动态IP并且Cookie的寿命未知,则会出现问题。

您也可以通过解析响应来获取cookie,但cookie是AES加密的,这会有点复杂。

最明智的解决方案是切换到没有这种保护的主机。
这是在thisthis问题中基本相同的问题的解决方案。

+0

是的,这是主机问题。我尝试了另一个托管网站www.hostinger.in,它运行良好。 –

+0

000webhostapp.com是另一个经过测试的选项 – Rick2047