![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
SimpleSocket 1.0 examples
Dependencies: EthernetNetIf SimpleSocket 1.0 mbed
supertweet.cpp
00001 #include "EthernetNetIf.h" 00002 #include "SimpleSocket.h" 00003 #include <ctype.h> 00004 00005 void encodeBase64(char *ibuf, char *obuf); 00006 int encodeFormUrl(char *s, char *t); 00007 00008 void supertweet() { 00009 EthernetNetIf eth; 00010 eth.setup(); 00011 ClientSocket client("66.180.175.246", 80); // api.supertweet.net 00012 00013 char user[16], password[16], message[64] = {}; 00014 printf("user => "); 00015 scanf("%s", user); 00016 printf("password => "); 00017 scanf("%s", password); 00018 printf("message => "); 00019 int c = 0; 00020 while (c < ' ' || 0x7E < c) 00021 c = getc(stdin); 00022 ungetc(c, stdin); 00023 for (int i = 0; i < sizeof(message) - 1 && (c = getc(stdin)) >= ' '; i++) 00024 message[i] = c; 00025 00026 char credential[48], credential2[64], message2[256]; 00027 00028 sprintf(credential, "%s:%s", user, password); 00029 encodeBase64(credential, credential2); 00030 encodeFormUrl(message, message2); 00031 00032 const char *request = 00033 "POST /1/statuses/update.xml HTTP/1.1\r\n" 00034 "Host: api.supertweet.net\r\n" 00035 "Authorization: Basic %s\r\n" 00036 "Content-Length: %d\r\n" 00037 "Content-Type: application/x-www-form-urlencoded\r\n" 00038 "\r\n" 00039 "status=%s"; 00040 00041 client.printf(request, credential2, strlen(message2) + 7, message2); 00042 printf(request, credential2, strlen(message2) + 7, message2); 00043 printf("\n"); 00044 00045 while (client) { 00046 if (client.available()) { 00047 while (client.available()) { 00048 char response[128] = {}; 00049 client.read(response, sizeof(response) - 1); 00050 printf("%s", response); 00051 } 00052 client.close(); 00053 } 00054 } 00055 printf("\ndone\n"); 00056 } 00057 00058 int encodeFormUrl(char *s, char *t) { 00059 char *head = t; 00060 for (char c; (c = *s) != 0; s++) 00061 switch (c) { 00062 case '\r': 00063 break; 00064 case ' ' : 00065 *t++ = '+'; 00066 break; 00067 default: 00068 t += sprintf(t, isalnum(c) ? "%c" : (c == '\n') ? "\r%c" : "%%%02X", c); 00069 } 00070 *t = '\0'; 00071 return t - head; 00072 } 00073 00074 void encodeBase64(char ibuf[], int length, char *obuf) { 00075 const char BASE64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 00076 int i, j; 00077 for (i = j = 0; j < length; j += 3, i += 4) { 00078 long a = ibuf[j] << 16 | (j + 1 < length ? ibuf[j + 1] << 8 : 0) | (j + 2 < length ? ibuf[j + 2] : 0); 00079 for (int k = 3; k >= 0; k--, a >>= 6) 00080 obuf[i + k] = (j + k - 1) < length ? BASE64[a & 63] : '='; 00081 } 00082 obuf[i] = '\0'; 00083 } 00084 00085 void encodeBase64(char *ibuf, char *obuf) { 00086 encodeBase64(ibuf, strlen(ibuf), obuf); 00087 }
Generated on Wed Jul 13 2022 16:35:12 by
![doxygen](doxygen.png)