Go Down

Topic: %2520 spazio inece che %20 (Read 1 time) previous topic - next topic

tarata

Ciao a tutti ho un problema :

dotazione:
nodemcu
sketch:

Code: [Select]


#include <ESP8266WiFi.h>


const char* ssid     = "rete";
const char* password = "pwd adsl";

const char* host = "dominio.it";

//------


//------
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 = "/track/requests.php?a=upload&u=test2&p=password&lat=000&long=0000&do=2016-10-16";

url += "%20";
url += "18:00:00&tn=nodemcu&db=8&ang=0.0&sp=0.0&alt=10.0";
////url += "";
//
// url += "";
// 

//-----------

//String encoded=urlencode(url);
//Serial.println(encoded);
////
//  String decoded=urldecode(encoded);
//  Serial.println(decoded);
  String decoded=(url);
  Serial.println(decoded);
//---------------


//String encoded=urlencode("Hello\nWorld");
//  Serial.println(encoded);
//
//  String decoded=urldecode(encoded);
//  Serial.println(decoded);
//----------
  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");
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }
 
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
 
  Serial.println();
  Serial.println("closing connection");
}







nello sketch dopo essermi connesso creo un url e lo passo ad una pagina php.

Tra la data e l' ora url deve avere uno spazio

ma il server restituisce pagina non trovata perchè in corrispondenza di %20 l'url finale è %2520

SukkoPera

Con tutti quei commenti non si capisce se usi o meno la funzione urlencode(). Se la usi, essa si occupa di codificare opportunamente i caratteri "strani", per cui non devi aggiungere %20 alla stringa, ma un vero e proprio spazio.
"Code is read much more often than it is written, so plan accordingly. Design for readability."

Guida rapida a ESP8266: https://goo.gl/kzh62E

tarata

ciao allora
1)se non la uso, e metto %20 (al posto dello spazio) l url generato è....%2520... e in print la pagina ovviamente non viene trovata
2) se non la uso e metto spazio la url viene troncata prima dello spazio
3)se la uso metto spazio la url viene troncata prima dello spazio

HELPPP

SukkoPera

Secondo me hai fatto casino da qualche parte, la soluzione giusta è: metti uno spazio normale nella stringa e chiamaci urlencode() sopra. Riprova così e poi posta lo sketch corretto, se ancora non funziona.
"Code is read much more often than it is written, so plan accordingly. Design for readability."

Guida rapida a ESP8266: https://goo.gl/kzh62E

tarata

allora questo il codice:

Code: [Select]

#include <ESP8266WiFi.h>

const char* ssid     = "adsl";
const char* password = "pwd";

const char* host = "dominio.it";

//------

String urldecode(String str)
{
   
    String encodedString="";
    char c;
    char code0;
    char code1;
    for (int i =0; i < str.length(); i++){
        c=str.charAt(i);
      if (c == '+'){
        encodedString+=' '; 
      }else if (c == '%') {
        i++;
        code0=str.charAt(i);
        i++;
        code1=str.charAt(i);
        c = (h2int(code0) << 4) | h2int(code1);
        encodedString+=c;
      } else{
       
        encodedString+=c; 
      }
     
      yield();
    }
   
   return encodedString;
}

String urlencode(String str)
{
    String encodedString="";
    char c;
    char code0;
    char code1;
    char code2;
    for (int i =0; i < str.length(); i++){
      c=str.charAt(i);
      if (c == ' '){
        encodedString+= '+';
      } else if (isalnum(c)){
        encodedString+=c;
      } else{
        code1=(c & 0xf)+'0';
        if ((c & 0xf) >9){
            code1=(c & 0xf) - 10 + 'A';
        }
        c=(c>>4)&0xf;
        code0=c+'0';
        if (c > 9){
            code0=c - 10 + 'A';
        }
        code2='\0';
        encodedString+='%';
        encodedString+=code0;
        encodedString+=code1;
        //encodedString+=code2;
      }
      yield();
    }
    return encodedString;
   
}

unsigned char h2int(char c)
{
    if (c >= '0' && c <='9'){
        return((unsigned char)c - '0');
    }
    if (c >= 'a' && c <='f'){
        return((unsigned char)c - 'a' + 10);
    }
    if (c >= 'A' && c <='F'){
        return((unsigned char)c - 'A' + 10);
    }
    return(0);
}


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 = "/track/requests.php?a=upload&u=test2&p=pwd&lat=000&long=000&do=2016-10-16 18:00:00&tn=nodemcu&db=8&ang=0.0&sp=0.0&alt=10.0";


Serial.print("stringa decoded ");
String urlencode=(url);
 
  Serial.println(urlencode);


 
  // This will send the request to the server
  client.print(String("GET ") + urlencode + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }
 
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
 
  Serial.println();
  Serial.println("closing connection");
}




questa la seriale:
Code: [Select]

Connecting to adsl
..
WiFi connected
IP address:
192.168.0.100
connecting to dominio.it
stringa decoded /track/requests.php?a=upload&u=test2&p=pwd&lat=000&long=000&do=2016-10-16 18:00:00&tn=nodemcu&db=8&ang=0.0&sp=0.0&alt=10.0
HTTP/1.1 301 Moved Permanently
Date: Wed, 19 Oct 2016 14:03:36 GMT
Server: Apache
Location: http://www.dominio.it/track/requests.php?a=upload&u=test2&p=pwd&lat=000&long=000&do=2016-10-16
Content-Length: 345
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://www.dominio.it/track/requests.php?a=upload&amp;u=test2&amp;p=pwd&amp;lat=000&amp;long=000&amp;do=2016-10-16">here</a>.</p>
</body></html>

closing connection




quindi come dicevo l url la tronca allo spazio

SukkoPera

#5
Oct 19, 2016, 04:19 pm Last Edit: Oct 19, 2016, 04:22 pm by SukkoPera
Solito copia&incolla senza avere idea di quel che stai facendo, vero? Prova questo:

Code: [Select]
String url = "/track/requests.php?a=upload&u=test2&p=pwd&lat=000&long=000&do=2016-10-16 18:00:00&tn=nodemcu&db=8&ang=0.0&sp=0.0&alt=10.0";

 // This will send the request to the server
 client.print(String("GET ") + urlencode(url) + " HTTP/1.1\r\n" +
              "Host: " + host + "\r\n" +
              "Connection: close\r\n\r\n");
"Code is read much more often than it is written, so plan accordingly. Design for readability."

Guida rapida a ESP8266: https://goo.gl/kzh62E

tarata

exit status 1
ambiguous overload for 'operator+' (operand types are 'String' and 'String(String)')

SukkoPera

Devi cancellare le righe in mezzo...
"Code is read much more often than it is written, so plan accordingly. Design for readability."

Guida rapida a ESP8266: https://goo.gl/kzh62E

tarata

ops avevo sbagliato ora carica ma comunque non va

 ho messoprima un

Serial.println(urlencode(url));

e la seriale stampa:
Code: [Select]

Connecting to adsl
..
WiFi connected
IP address:
192.168.0.100
connecting to dominio.it
%2Ftrack%2Frequests%2Ephp%3Fa%3Dupload%26u%3Dtest2%26p%3Dpwd%26lat%3D000long%3D000%26do%3D2016%2D10%2D16+18%3A00%3A00%26tn%3Dnodemcu%26db%3D8%26ang%3D0%2E0%26sp%3D0%2E0%26alt%3D10%2E0
HTTP/1.1 400 Bad Request
Date: Wed, 19 Oct 2016 14:38:28 GMT
Server: Apache
Content-Length: 226
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
</body></html>

closing connection





copio & incollo a volte non passo dal libretto delle istruzioni mi ci butto di testa e molte volte mi fermo poi ci ritorno ma non mollo ;-)

SukkoPera

Ho capito, fai questo:

Code: [Select]
String url = "/track/requests.php?a=upload&u=test2&p=pwd&lat=000&long=000&do=" + urlencode ("2016-10-16 18:00:00") + "&tn=nodemcu&db=8&ang=0.0&sp=0.0&alt=10.0";

 // 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");




"Code is read much more often than it is written, so plan accordingly. Design for readability."

Guida rapida a ESP8266: https://goo.gl/kzh62E

tarata

anche cosi converte i : e - allora ho fatto cosi


Code: [Select]

String url = "/track/requests.php?a=upload&u=test2&p=pwd&lat=000&long=000&do=2016-10-16" + urlencode (" ") + "18:00:00&tn=nodemcu&db=8&ang=0.0&sp=0.0&alt=10.0";


ma la seriale stampa
Code: [Select]

....&amp;do=2016-10-16+18:00:00&a.....

SukkoPera

#11
Oct 19, 2016, 05:42 pm Last Edit: Oct 19, 2016, 05:42 pm by SukkoPera
Ma è giusto che converta i : e i -, non ti preoccupare, verranno opportunamente decodificati "dall'altra parte". Inoltre gli spazi possono essere convertiti in +, è come %20, non ti preoccupare neanche di questo.
"Code is read much more often than it is written, so plan accordingly. Design for readability."

Guida rapida a ESP8266: https://goo.gl/kzh62E

tarata

Code: [Select]
HTTP/1.1 301 Moved Permanently
Date: Wed, 19 Oct 2016 15:46:14 GMT
Server: Apache
Location: http://www.dominio.it/track/requests.php?a=upload&u=test2&p=pwd&lat=000&long=000&do=2016%252D10%252D16+18%253A00%253A00&tn=nodemcu&db=8&ang=0.0&sp=0.0&alt=10.0
Content-Length: 419
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://www.dominio.it/track/requests.php?a=upload&amp;u=test2&amp;p=pwd&amp;lat=000&amp;long=000&amp;do=2016%252D10%252D16+18%253A00%253A00&amp;tn=nodemcu&amp;db=8&amp;ang=0.0&amp;sp=0.0&amp;alt=10.0">here</a>.</p>
</body></html>

closing connection

SukkoPera

Beh, secondo me è giusto. Hai verificato bene cosa c'è di diverso nell'indirizzo che ti propone? Hai provato a copiare l'URL e incollarlo in un browser?
"Code is read much more often than it is written, so plan accordingly. Design for readability."

Guida rapida a ESP8266: https://goo.gl/kzh62E

tarata

Be che dirti hai ragione....
allora l' url generata se la incollo nel browser funziona e la pagina php passa i valori nel db...
ma url se invece la passa nodemcu crea dei caratteri "&amp;"  al posto "&" e quindi la pagina non viene trovata sul server

Code: [Select]

connecting to dominio.it
HTTP/1.1 301 Moved Permanently
Date: Thu, 20 Oct 2016 06:04:44 GMT
Server: Apache
Location: http://www.dominio.it/track/requests.php?a=upload&u=test2&p=pwd&lat=000&long=000&do=2016%252D10%252D16+18%253A00%253A00&tn=nodemcu&db=8&ang=0.0&sp=0.0&alt=10.0
Content-Length: 419
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://www.dominio.it/track/requests.php?a=upload&amp;u=test2&amp;p=pwd&amp;lat=000&amp;long=000&amp;do=2016%252D10%252D16+18%253A00%253A00&amp;tn=nodemcu&amp;db=8&amp;ang=0.0&amp;sp=0.0&amp;alt=10.0">here</a>.</p>
</body></html>

closing connection



bisognerebbe fare un urlencode solo per &?

Go Up