Saludos a todos.
Tengo una placa esp8266 que se conecta como cliente a un servidor externo a través del una red wifi. Funciona.
Pero el servidor me devuelve una página que incluye una sección de javaScript. El servidor detecta que el cliente (en este caso la esp8266) no soporta javaScript y me redirige a una página de error. Aunque la info que necesito está en la sección estática de la página.
Consulta:
¿Puedo saltar ese chequeo de alguna manera?
Código:
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
void setup() {
Serial.begin(115200);
delay(10);
conectaWifi();
}
void loop() {
// Send an HTTP GET request
if ((millis() - lastTime) > timerDelay) {
// Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
String serverPath = "ladireccion";
String resultado = httpGETRequest(serverPath.c_str());
Serial.println(resultado);
} else {
Serial.println("WiFi Disconnected");
conectaWifi();
}
lastTime = millis();
}
}
String httpGETRequest(const char* serverName) {
WiFiClient client;
HTTPClient http;
// Your IP address with path or Domain name with URL path
http.begin(client, serverName);
// Send HTTP POST request
int httpResponseCode = http.GET();
String payload = "{}";
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
return payload;
}
Soy nuevo y no se si he planteado bien la pregunta, o si la he colocado en el lugar adecuado. Disculpas de antemano.
You can't skip the detection on the server. It is likely based on the User-Agent header sent in the request. So you can just lie and say you're using a browser that supports JavaScript.
If you visit httpbin.org/headers, it will return all the request headers. For example
You can actually tell quite a bit about the browser and OS in use. There are also seemingly redundant or superseding claims. That's a result of how it evolved over thirty years.
HTTPClients vary in how to set the request headers. For ESP8266, their PostHTTPClient example shows calling addHeader immediately after begin. However, that function explicitly does not allow setting "User-Agent" that way, because it handles it separately, using the setUserAgent function. (It always sends one, unless it is empty, a zero-length string.)
You can use the string quoted above, or the one from whatever browser you have. If one of those works, you might even try to pare down the string to "Mozilla/5.0", or ""
Hola, muchas gracias por la respuesta.
Si, conectaWifi() contiene ese codigo y funciona.
Mi sospecha es que la web solicita algún tipo de dato o acción al explorador sin el cual no me redirecciona a la página que solicito. ¿Puede ser correcto?
La web está alojada en un servidor gratuito, (infinityfree).
La respuesta que obtengo es esta:
22:19:19.360 -> HTTP Response code: 200
22:19:19.360 ->
<html>
<body>
<script type="text/javascript" src="/aes.js" ></script>
<script>
function toNumbers(d){var e=[];
d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++) e+=(16>d[f]?"0":"")+d[f].toString(16);
return e.toLowerCase()}.
var a=toNumbers("f655ba9d09a112d4968c63579db590b4"), b=toNumbers("98344c2eee86c3994890592585b49f80"), c=toNumbers("1a02b3bc342529a22ea7236b229b307e");
document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+";
expires=Thu, 31-Dec-37 23:55:55 GMT;
path=/";
location.href="http://makina.free.nf/consulta.php?i=1";
</script>
<noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support
</noscript>
</body>
</html>
Como he comentado, el problema me ha surgido por esa politica del servisor que solo permite el acceso a exploradores que soporten javascript (para cuentas gratuitas de infinityfree).
He cambiado a otro servidor (awardspace) y ya accedo sin restricciones a los archivos.
Gracias por la respuesta.