当前位置:  开发笔记 > 开发工具 > 正文

ESP8266 WiFiClient简单的HTTP GET

如何解决《ESP8266WiFiClient简单的HTTPGET》经验,为你挑选了1个好方法。

我正在研究使用ESP8266和ESP8266WiFi库阅读网页的简单问题.

我在示例中仅更改了几行,但不知道问题是什么.那是我的代码:

include 

const char* ssid     = "WiwoNET";
const char* password = "xxxxxxx";

const char* host = "https://pure-caverns-1350.herokuapp.com";

void setup() {
  Serial.begin(115200);
  delay(10);

  // We start by connecting to a WiFi network

  Serial.println();
  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());
}

int value = 0;

void loop() {
  delay(5000);
  ++value;

  Serial.print("connecting to ");
  Serial.println(host);

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  // We now create a URI for the request
  String url = "/stan";

  Serial.print("Requesting URL: ");
  Serial.println(url);

  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");
  delay(10);

  // Read all the lines of the reply from server and print them to Serial
  Serial.println("Respond:");
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }

  Serial.println();
  Serial.println("closing connection");
}

我在串口监视器中看到的是:

Connecting to WiwoNET
.......
WiFi connected
IP address: 
192.168.0.111
connecting to https://pure-caverns-1350.herokuapp.com
Requesting URL: /stan
Informacja zwrotna:
HTTP/1.1 400 Bad Request
Connection: close
Server: Cowboy
Date: Thu, 03 Dec 2015 23:38:59 GMT
Content-Length: 0


closing connection

我正在查看heroku的日志,没有任何东西在那里显示.提前感谢您提供任何帮助.



1> Marcel Stör..:

你一定是在https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiClient/WiFiClient.ino上关注这个例子.

不过,你错过了一件至关重要的作品.该host值不得以URI样式的方案作为前缀,例如http://https://.再看一下这个例子并使用

const char* host = "pure-caverns-1350.herokuapp.com";

代替.

如果curl -v http://pure-caverns-1350.herokuapp.com/stan在您的控制台中运行,您可以很好地了解HTTP下的情况.

推荐阅读
帆侮听我悄悄说星星
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有