2016-01-02 81 views
0

我正在使用ESP8266模塊。我成功創建了一個草圖,它向我的本地服務器發送一個HTTP請求。服務器發送響應,但是,我無法在Arduino IDE的串行監視器中顯示它。我無法在Internet上找到任何地方,如何顯示收到的消息。唯一能夠顯示的是整個GET請求。ESP8266顯示器收到http響應

它甚至有可能得到,解析並顯示服務器的響應?如果是這樣,任何人都可以提供代碼示例?

回答

0
 HTTPClient http; 

     USE_SERIAL.print("[HTTP] begin...\n"); 
     // configure traged server and url 
     //http.begin("https://192.168.1.12/test.html", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS 
     http.begin("http://192.168.1.12/test.html"); //HTTP 

     USE_SERIAL.print("[HTTP] GET...\n"); 
     // start connection and send HTTP header 
     int httpCode = http.GET(); 

     // httpCode will be negative on error 
     if(httpCode) { 
      // HTTP header has been send and Server response header has been handled 
      USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode); 

      // file found at server 
      if(httpCode == HTTP_CODE_OK) { 
       String payload = http.getString(); 
       // print only part of the string 
       USE_SERIAL.println(payload.substring(0,200)); 
      } 
     } else { 
      USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); 
     } 

     http.end(); 

這對你有用嗎? 關鍵是:payload.substring(0,200)

HTH