Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface SimpleSocket mbed-rtos mbed
Fork of SimpleSocketExamples by
supertweet.cpp
00001 #include "SimpleSocket.h" 00002 #include <ctype.h> 00003 00004 void encodeBase64(char *ibuf, char *obuf); 00005 int encodeFormUrl(char *s, char *t); 00006 00007 void supertweet() { 00008 ClientSocket client("66.180.175.246", 80); // api.supertweet.net 00009 00010 char user[16], password[16], message[64] = {}; 00011 printf("user => "); 00012 scanf("%s", user); 00013 printf("password => "); 00014 scanf("%s", password); 00015 printf("message => "); 00016 int c = 0; 00017 while (c < ' ' || 0x7E < c) 00018 c = getc(stdin); 00019 ungetc(c, stdin); 00020 for (int i = 0; i < sizeof(message) - 1 && (c = getc(stdin)) >= ' '; i++) 00021 message[i] = c; 00022 00023 char credential[48], credential2[64], message2[256]; 00024 00025 sprintf(credential, "%s:%s", user, password); 00026 encodeBase64(credential, credential2); 00027 encodeFormUrl(message, message2); 00028 00029 const char *request = 00030 "POST /1/statuses/update.xml HTTP/1.1\r\n" 00031 "Host: api.supertweet.net\r\n" 00032 "Authorization: Basic %s\r\n" 00033 "Content-Length: %d\r\n" 00034 "Content-Type: application/x-www-form-urlencoded\r\n" 00035 "\r\n" 00036 "status=%s"; 00037 00038 client.printf(request, credential2, strlen(message2) + 7, message2); 00039 printf(request, credential2, strlen(message2) + 7, message2); 00040 printf("\n"); 00041 00042 while (client) { 00043 if (client.available()) { 00044 while (client.available()) { 00045 char response[128] = {}; 00046 client.read(response, sizeof(response) - 1); 00047 printf("%s", response); 00048 } 00049 client.close(); 00050 } 00051 } 00052 printf("\ndone\n"); 00053 } 00054 00055 int encodeFormUrl(char *s, char *t) { 00056 char *head = t; 00057 for (char c; (c = *s) != 0; s++) 00058 switch (c) { 00059 case '\r': 00060 break; 00061 case ' ' : 00062 *t++ = '+'; 00063 break; 00064 default: 00065 t += sprintf(t, isalnum(c) ? "%c" : (c == '\n') ? "\r%c" : "%%%02X", c); 00066 } 00067 *t = '\0'; 00068 return t - head; 00069 } 00070 00071 void encodeBase64(char ibuf[], int length, char *obuf) { 00072 const char BASE64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 00073 int i, j; 00074 for (i = j = 0; j < length; j += 3, i += 4) { 00075 long a = ibuf[j] << 16 | (j + 1 < length ? ibuf[j + 1] << 8 : 0) | (j + 2 < length ? ibuf[j + 2] : 0); 00076 for (int k = 3; k >= 0; k--, a >>= 6) 00077 obuf[i + k] = (j + k - 1) < length ? BASE64[a & 63] : '='; 00078 } 00079 obuf[i] = '\0'; 00080 } 00081 00082 void encodeBase64(char *ibuf, char *obuf) { 00083 encodeBase64(ibuf, strlen(ibuf), obuf); 00084 }
Generated on Thu Jul 14 2022 21:25:54 by
 1.7.2
 1.7.2 
    