10 years, 9 months ago.

mBed Post HTTPS URL

Dear All,

I would like to know does TCPSocketConnection support HTTPS url?

https://docs.google.com/forms/d/1p1euWn1eR333jsE9j2MrSyeK7pGo9BacgTBtOLv7y-c/formResponse?entry.2105883526=hello&submit=Submit

Above is the url for post data to a google spreadsheet. It can be done directly from the computer by just typing this url into a web browser then the data (hello) will update in spreadsheet cell. When i use mbed to post it it doesn't work..

Post HTTPS URL

#include "mbed.h"
#include "EthernetInterface.h"

DigitalOut  led(LED1);

int main() {
    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();
    printf("IP Address is %s\n", eth.getIPAddress());
    
    TCPSocketConnection sock;    
    //sock.connect("mbed.org", 80);
    sock.connect("https://docs.google.com", 80); 
           
    //char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt\n\n";
    char http_cmd[] = "POST /forms/d/1p1euWn1eR333jsE9j2MrSyeK7pGo9BacgTBtOLv7y-c/formResponse?entry.2105883526=hello&submit=Submit";
        
    sock.send_all(http_cmd, sizeof(http_cmd)-1);
    
    char buffer[300];
    int ret;
    while (true) {
        ret = sock.receive(buffer, sizeof(buffer)-1); 
        if (ret <= 0)
            break;
        buffer[ret] = '\0';
        printf("Received %d chars from server:\n%s\n", ret, buffer);
    }
      
    sock.close();
    
    eth.disconnect();
    
    while(1) { led=0; wait(0.5); led=1; wait(0.5); }
}

I have tired it few days and play around difference url format but still doesn't work. I had do some google search and found there is many people facing this kind of problem but they are using Arduino. But they have other alternative way to do that which is rely on third party (eg: pushingbox.com) that support Arduino. Most probably is connect to https issue.

May i know does any mBed members have this experience? How to overcome this problem?

Thank you, Wq

Did you ever find a solution to this problem?

posted by Amol P 02 Mar 2016

2 Answers

10 years, 9 months ago.

Hi Wen Qian,

TcpSocketConnection is indifferent to protocols.

sock.connect("https://docs.google.com", 80);

This won't work, the first argument needs to be a resolvable hostname or IP address. If https is required then you need to use the port for https which is 443. However https is more complex than http because it is encrypted. There are a few libraries on mbed that people have used for SSL/TLS support.

https://mbed.org/forum/mbed/topic/3823/ this discussion mentions a few different libraries.

Accepted Answer
Wen qian
poster
10 years, 9 months ago.

Hi Stephen Paulger,

Thanks for your reply.

After gone through the link that you shared and do some quick mbed search. I feel it is a bit complicated when using SSL/TLS and i still need more time to find out how it works. But now then realize https is encrypted.

Before that i would like to use the TCPSocket_HelloWorld sample code to test see whether can get any correct outcome. In the previous thread you have point out a few mistake in my sample code. I'm not sure what's the host name and IP for google spreadsheet but i guess the host name should be "google.com". Please correct me if i'm wrong.

POST https

#include "mbed.h"
#include "EthernetInterface.h"
 
DigitalOut  led(LED1);
 
int main() {
    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();
    printf("IP Address is %s\n", eth.getIPAddress());
    
    TCPSocketConnection sock;    
    //sock.connect("mbed.org", 80);
    sock.connect("google.com", 443); 
           
    //char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt\n\n";
    char http_cmd[] = "POST /docs.google.com/forms/d/1p1euWn1eR333jsE9j2MrSyeK7pGo9BacgTBtOLv7y-c/formResponse?entry.2105883526=hello&submit=Submit";
        
    sock.send_all(http_cmd, sizeof(http_cmd)-1);
    
    char buffer[300];
    int ret;
    while (true) {
        ret = sock.receive(buffer, sizeof(buffer)-1); 
        if (ret <= 0)
            break;
        buffer[ret] = '\0';
        printf("Received %d chars from server:\n%s\n", ret, buffer);
    }
      
    sock.close();
    
    eth.disconnect();
    
    while(1) { led=0; wait(0.5); led=1; wait(0.5); }
}

Output: Only show the ip

Quote:

IP Address is 192.168.137.3

If i change the port to 80

POST https

 sock.connect("google.com", 80); 

Output: Some error content!!?

Quote:

IP Address is Received 299 chars from server: HTTP/1.0 411 Length Required

Content-Type: text/html; charset=UTF-8

Content-Length: 934

Date: Thu, 08 Aug 2013 16:06:23 GMT

Server: GFE/2.0

<!DOCTYPE html> <html lang=en> <meta charset=utf-8> <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width"> <title>Err Received 299 chars from server: or 411 (Length Required)!!1</title> <style>

  • {margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(www.google.com/images/errors/robot.png) Received 299 chars from server: 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}} </style> <a href=www.google.com/><img src=www.google.com/imag Received 184 chars from server: es/errors/logo_sm.gif alt=Google></a> <p><b>411.</b> <ins>That’s an error.</ins> <p>POST requests require a <code>Content-length</code> header. <ins>That’s all we know.</ins>

Is it this is the expected output?

Thank you, Wq

If HTTPS is not required you might have more luck trying to use a HTTP library rather than hard coding the http protocol yourself. There are a couple you can try on this page http://mbed.org/handbook/TCP-IP-protocols-and-APIs

posted by Stephen Paulger 08 Aug 2013