Go Down

Topic: 302 Moved: The document has moved <> here (Read 205 times) previous topic - next topic

deeperrr

Hello there,
i have an arduino uno and a ethernet shield. When i run the example sketch WebClient i always get the same non satisfying result:
Code: [Select]
connecting...
connected
HTTP/1.1 302 Found
Location: http://www.google.de/search?q=arduino&gws_rd=cr&dcr=0&ei=BS_fWbuNEIXTwALmoZfABg
Cache-Control: private
Content-Type: text/html; charset=UTF-8
P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
Date: Thu, 12 Oct 2017 08:59:49 GMT
Server: gws
Content-Length: 288
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Set-Cookie: NID=114=p0LtDSNpjl0PWLOaS-3qETxRmIxOu8G1vf3olBw9EZQ4xv9cSv-aY-d99imTXv84YnDsRVuQUUZ1zLJny590RWQZBpfXchbQwnjI-ZQooTI68im3-RaYZjyMi_BUu9K6; expires=Fri, 13-Apr-2018 08:59:49 GMT; path=/; domain=.google.com; HttpOnly
Connection: close

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.de/search?q=arduino&amp;gws_rd=cr&amp;dcr=0&amp;ei=BS_fWbuNEIXTwALmoZfABg">here</A>.
</BODY></HTML>

disconnecting.



Does somebody know whats going on here as I am new to this topic?

PaulS

Quote
Does somebody know whats going on here as I am new to this topic?
The server told you EXACTLY what is going on.

The code you didn't post is requesting a URL that the server knows no longer exists. It is kind enough to tell you that the URL is no longer valid AND to tell you where the file moved to.

If you need help fixing your code, POST YOUR CODE.
The art of getting good answers lies in asking good questions.

deeperrr

I tried that. When i run the sketch with the url the server told me I get an error message "Connection failed".
Code: [Select]
/*
  Web client

 This sketch connects to a website (http://www.google.com)
 using an Arduino Wiznet Ethernet shield.

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13

 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe, based on work by Adrian McEwen

 */

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xB3, 0xEF, 0xF7, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "www.google.com";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.1");
    client.println("Host: www.google.com");
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop() {
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}

PaulS

Is that the code that causes the error? Or is that code that you modified, to try to solve the problem?

What country are you in? It appears as though you are trying to access the US version of google, and that google wants you to use a version appropriate for your country.
The art of getting good answers lies in asking good questions.

deeperrr

U are right, the issue was accessing google.com from germany. I now used google.de and it worked.

But im getting similar messages when speaking to other servers with this code:
Code: [Select]
/*
  Web client

 This sketch connects to a website (http://www.google.com)
 using an Arduino Wiznet Ethernet shield.

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13

 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe, based on work by Adrian McEwen

 */

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xB3, 0xEF, 0xF7, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "fiw.fhws.de";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /aktuelles/news.html HTTP/1.1");
    client.println("Host: fiw.fhws.de");
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop() {
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}

and i get the following result
Code: [Select]
connecting...
connected
HTTP/1.1 301 Moved Permanently
Date: Thu, 12 Oct 2017 09:42:38 GMT
Server: Apache/2.4.7 (Ubuntu)
Location: https://fiw.fhws.de/aktuelles/news.html
Content-Length: 323
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="https://fiw.fhws.de/aktuelles/news.html">here</a>.</p>
<hr>
<address>Apache/2.4.7 (Ubuntu) Server at fiw.fhws.de Port 80</address>
</body></html>

disconnecting.


The only difference i see is that the url in my sketch hasnt "https://". Could that be the crucial aspect?
But when I set the URL to https://... the arduino cant even connect

PaulS

Quote
Could that be the crucial aspect?
Yes. The URL has been moved to a secure server. The Arduino can't do https. It can only do http.
The art of getting good answers lies in asking good questions.

deeperrr

So what ways are there to perform https requests and get the data to the arduino?
I thought of a raspberry pi, but i rather dont want to spend 40 bucks on that project

gdsports

Arduino for ESP8266 can do HTTPS. Use 2.4.0 or newer.


JustGoFly

They are redirecting you to the same link, inserting an SSL parameter and encrypting some information into cookies.  They explain what they are doing here:
https://support.google.com/accounts/answer/151657?hl=en

Usually the 302 is used to redirect HTTP to HTTPS, but it looks like google has found a way to do some authentication encryption using ssl encrypted cookies. Cool beans.

Vinnie

Go Up