SuperTweet I18n version
Dependencies: EthernetNetIf mbed
main.cpp@1:6699b22eb872, 2011-11-06 (annotated)
- Committer:
- yamaguch
- Date:
- Sun Nov 06 13:40:37 2011 +0000
- Revision:
- 1:6699b22eb872
- Parent:
- 0:e980b24fd680
0.91
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yamaguch | 0:e980b24fd680 | 1 | #include "EthernetNetIf.h" |
yamaguch | 0:e980b24fd680 | 2 | #include "SimpleSocket.h" |
yamaguch | 0:e980b24fd680 | 3 | #include "utils.h" |
yamaguch | 0:e980b24fd680 | 4 | |
yamaguch | 1:6699b22eb872 | 5 | /* |
yamaguch | 1:6699b22eb872 | 6 | Twitter client thru api.supertweet.net proxy server |
yamaguch | 1:6699b22eb872 | 7 | |
yamaguch | 1:6699b22eb872 | 8 | In order to tweet thru api.supertweet, prepare a text file "tweet.txt" in |
yamaguch | 1:6699b22eb872 | 9 | mbed local file system (flash), containing the following 3 lines: |
yamaguch | 1:6699b22eb872 | 10 | |
yamaguch | 1:6699b22eb872 | 11 | user:<user account> |
yamaguch | 1:6699b22eb872 | 12 | password:<password for supertweet.net> |
yamaguch | 1:6699b22eb872 | 13 | message:<unicode text in UTF-8> |
yamaguch | 1:6699b22eb872 | 14 | |
yamaguch | 1:6699b22eb872 | 15 | */ |
yamaguch | 1:6699b22eb872 | 16 | |
yamaguch | 0:e980b24fd680 | 17 | int main() { |
yamaguch | 0:e980b24fd680 | 18 | EthernetNetIf eth; |
yamaguch | 0:e980b24fd680 | 19 | eth.setup(); |
yamaguch | 0:e980b24fd680 | 20 | |
yamaguch | 0:e980b24fd680 | 21 | ClientSocket client("api.supertweet.net", 80); |
yamaguch | 1:6699b22eb872 | 22 | if (!client) error("connection failed\n"); |
yamaguch | 0:e980b24fd680 | 23 | |
yamaguch | 0:e980b24fd680 | 24 | LocalFileSystem local("local"); |
yamaguch | 0:e980b24fd680 | 25 | char user[16], password[32], message[420]; |
yamaguch | 0:e980b24fd680 | 26 | |
yamaguch | 0:e980b24fd680 | 27 | getValue("/local/tweet.txt", "user", user); |
yamaguch | 0:e980b24fd680 | 28 | getValue("/local/tweet.txt", "password", password); |
yamaguch | 0:e980b24fd680 | 29 | getValue("/local/tweet.txt", "message", message); |
yamaguch | 0:e980b24fd680 | 30 | |
yamaguch | 0:e980b24fd680 | 31 | char credential[48], credential2[64], message2[1260]; |
yamaguch | 0:e980b24fd680 | 32 | sprintf(credential, "%s:%s", user, password); |
yamaguch | 0:e980b24fd680 | 33 | encodeBase64(credential, credential2); |
yamaguch | 0:e980b24fd680 | 34 | encodeFormUrl(message, message2); |
yamaguch | 0:e980b24fd680 | 35 | |
yamaguch | 0:e980b24fd680 | 36 | const char *request = |
yamaguch | 0:e980b24fd680 | 37 | "POST /1/statuses/update.xml HTTP/1.1\r\n" |
yamaguch | 0:e980b24fd680 | 38 | "Host: api.supertweet.net\r\n" |
yamaguch | 0:e980b24fd680 | 39 | "Authorization: Basic %s\r\n" |
yamaguch | 0:e980b24fd680 | 40 | "Content-Length: %d\r\n" |
yamaguch | 0:e980b24fd680 | 41 | "Content-Type: application/x-www-form-urlencoded\r\n" |
yamaguch | 0:e980b24fd680 | 42 | "\r\n" |
yamaguch | 0:e980b24fd680 | 43 | "status="; |
yamaguch | 0:e980b24fd680 | 44 | |
yamaguch | 0:e980b24fd680 | 45 | client.printf(request, credential2, strlen(message2) + 7); |
yamaguch | 0:e980b24fd680 | 46 | client.write(message2, strlen(message2)); |
yamaguch | 0:e980b24fd680 | 47 | printf(request, credential2, strlen(message2) + 7); |
yamaguch | 0:e980b24fd680 | 48 | printf("%s", message2); |
yamaguch | 0:e980b24fd680 | 49 | printf("\n"); |
yamaguch | 0:e980b24fd680 | 50 | |
yamaguch | 0:e980b24fd680 | 51 | while (client) { |
yamaguch | 0:e980b24fd680 | 52 | if (client.available()) { |
yamaguch | 0:e980b24fd680 | 53 | while (client.available()) { |
yamaguch | 0:e980b24fd680 | 54 | char response[128] = {}; |
yamaguch | 0:e980b24fd680 | 55 | client.read(response, sizeof(response) - 1); |
yamaguch | 0:e980b24fd680 | 56 | printf("%s", response); |
yamaguch | 0:e980b24fd680 | 57 | } |
yamaguch | 0:e980b24fd680 | 58 | client.close(); |
yamaguch | 0:e980b24fd680 | 59 | } |
yamaguch | 0:e980b24fd680 | 60 | } |
yamaguch | 0:e980b24fd680 | 61 | |
yamaguch | 0:e980b24fd680 | 62 | printf("\ndone\n"); |
yamaguch | 0:e980b24fd680 | 63 | } |