[SOLVED] Using Ethernet R3 shield to access https site

I have an Arduino UNO and a (somewhat old) Ethernet R3 shield that I use as client to get data from a web server. I'm having trouble with https sites.

Example: When I go to a http site like example.com (http://www.example.com) it works as intented. When I go to a https site like mysite.com (https://mysite.com) it doesn't work. I connect like so:

#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[] = {  0x90, 0xA2, 0xDA, 0x0F, 0x5A, 0x7D };
char server[] = "mysite.com";

// 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() {
  // start the serial library:
  Serial.begin(9600);
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  // 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 / HTTP/1.1");
    client.println("Host: mysite.com");
    client.println("Connection: close");
    client.println();
  }
  else {
    // kf 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:
    for(;;)
      ;
  }
}

I get this reply:

HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=UTF-8
Location: https://mysite.com/
Server: Microsoft-IIS/10.0
Date: Tue, 26 Jan 2021 15:03:08 GMT
Connection: close
Content-Length: 141

<head><title>Document Moved</title></head>
<body><h1>Object Moved</h1>This document may be found <a HREF="https://mysite.com/">here</a></body>

I have to been able to figure how to change the code so that I can connect to the https site. Any ideas?

HTTPS uses encryption. I do not know where the encryption/decryption is done on your side of the system. This is what you need to figure out first. If it is on the Arduino (which I suspect), I would be surprised if you could get this to work. Encryption requires quite some computation and your Arduino Uno is not good at that.

Try some Google e.g. HTTPS Arduino or HTTPS ESP32

I have an Arduino UNO and a (somewhat old) Ethernet R3 shield that I use as client to get data from a web server. I'm having trouble with https sites.

The UNO is not able to do the encryption necessary to support TLS of HTTPS. For one it's processor isn't fast enough to do all the calculations in a reasonable time but far more important is that it hasn't enough RAM to hold the information necessary to do that encryption.

I have to been able to figure how to change the code so that I can connect to the https site. Any ideas?

It's not possible. If you need to access HTTPS siites, change the board. ESP (both ESP32 and ESP8266) support HTTPS by default. If you want to do serious stuff using TLS you should use a full-fledged operating system (p.e. Linux) to be able to catch up with security changes. Today TLS1.0 is often denied because it isn't secure enough anymore. In the future elliptic curve cryptography will get more important and sooner or later the cheap embedded MCUs will not be able to connect anymore to servers enforcing newer standards.

Good answers. Thanks a lot.

But also disappointing. All the sites I need to interact with are https. And most are today anyway.

I need to buy something new. I also would like to get rid of the cable. I’m considering Arduino MKR WiFi 1010 (or 1000). As far as I have been able to figure out it should be able to access https sites. Is that so?

Solved! I bought a Arduino MKR WiFi 1010 and used this example: WiFi SSL Client

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.