Arduino GSM Shield WebClient 302

2019-07-04 14:37发布

问题:

I put an arduino mega and the Arduino GSM Shield with a Fyve(vodafone)- Sim together. Long term I want to add a GPS to send position-data via the GSM module to data.sparkfun.com. In order to get my code running, I started with the GsmWebClient example from arduino. The problem is, that I constantly get the "HTTP/1.1 302 Found - Error". So apparently I get redirected. Got the feeling that there has to be a pretty much easy solution, but I cant figure it out. Basically read through the whole internet. I really dont know wth is going on and feel rather stupid now.

If i change the APN to web.vodafone.de GSM and GPRS connect, but the client doesnt.

Here comes the code and the response from the serial:

// libraries
#include <GSM.h>


// PIN Number
#define PINNUMBER "****"

// APN data
#define GPRS_APN       "event.vodafone.de" // replace your GPRS APN
#define GPRS_LOGIN     ""    // replace with your GPRS login
#define GPRS_PASSWORD  "" // replace with your GPRS password

// initialize the library instance
GSMClient client;
GPRS gprs;
GSM gsmAccess;

// URL, path & port (for example: arduino.cc)
char server[] = "arduino.cc";
char path[] = "/asciilogo.txt";
int port = 80; // port 80 is the default for HTTP

void setup() {
  // initialize 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
  }

  Serial.println("Starting Arduino web client.");
  // connection state
  boolean notConnected = true;

  // After starting the modem with GSM.begin()
  // attach the shield to the GPRS network with the APN, login and password
  while (notConnected) {
    Serial.println("connecting gsm");
    if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
          Serial.println("gsm connected");
          delay(1000);
          Serial.println("connecting gprs");
          if (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY) {
              Serial.println("gprs connected");
              notConnected = false;
        }
        else {
             Serial.println("gprs Not connected");
             delay(1000);
    }
      }
     else {
      Serial.println("gsm Not connected");
      delay(1000);
    }
  }

  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, port)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /asciilogo.txt HTTP/1.1");
    client.print("Host: ");
    client.println("www.arduino.cc");
    client.println("Connection: close");
    client.println();


  }
}

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.available() && !client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    for (;;)
      ;
  }
}

Starting Arduino web client.

connecting gsm

gsm connected

connecting gprs

gprs connected

connecting...

connected

HTTP/1.1 302 Found

Date: Thu, 23 Feb 2017 18:13:45 GMT

Server: Apache

Connection: close

Location: https://web.vodafone.de/sbb/redirectToLandingPage?lyt=vodafone&SESSION_TARGET_URL=http%3A%2F%2Fwww.arduino.cc%2Fasciilogo.txt

Content-Length: 0

Vary: User-Agent

Cache-Control: no-transform

Content-Type: text/plain; charset=ISO-8859-1

disconnecting.

Well, there you have it. I hope someone can help me here, that stuff is driving me crazy.

Regards Arne

回答1:

I just got feeling that Vodafone checks http-packet headers and if there is something that they think suspicious, they redirect them. Maybe you could try to add information to http-header, like user-agent?

    // if you get a connection, report back via serial:
    if (client.connect(server, port)) {
            Serial.println("connected");
            // Make a HTTP request:
            client.println("GET /asciilogo.txt HTTP/1.1");
            client.print("Host: ");
            client.println("www.arduino.cc");
            client.println("User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko");
            client.println("Connection: close");
            client.println();
    }

Example and info about http-headers

Discussion about Vodafone proxy detection and redirection



回答2:

Well guys, I solved the miracle. As stupid as it sounds: There was no tariff for internet chosen on the SIM. I got it from my supervisor and was sure it was enabled already. It wasn't. Failed quiet hard on that one.