As I might have mentioned before, I'm trying to write a Twitter library. Beyond OAuth being confusing -- but I think I've got it now -- I'm having trouble actually connecting to twitter. I'm using anarki for the moment, but this code works in stock arc3.1 also. Add this to ac.scm: (xdef tcp-connect (lambda (host port)
(let-values ([(from-server to-server)
(tcp-connect host port)])
(list from-server to-server))))
Then start up Arc, and let's define a utility function: (def write-message (host message)
(let (from-server to-server) (tcp-connect host 80)
(disp message to-server)
(close to-server)
(whilet line (readline from-server)
(prn line))
(close from-server)))
So now we can do fun things like this: arc> (write-message "www.google.com"
"GET / HTTP/1.1\r\nHost:www.google.com\r\n\r\n")
HTTP/1.1 200 OK
Date: Wed, 07 Mar 2012 16:42:16 GMT
Any results from the server are cut for readability. That's fine, let's try twitter now: arc> (write-message "www.twitter.com"
"GET / HTTP/1.1\r\nHost:www.twitter.com\r\n\r\n")
nil
Weird. Maybe they don't like www in their urls? arc> (write-message "twitter.com"
"GET / HTTP/1.1\r\nHost:twitter.com\r\n\r\n")
nil
Sometimes I've gotten a result from one of these, but usually nothing: arc> (write-message "www.twitter.com"
"GET / HTTP/1.1\r\nHost: www.twitter.com\r\n\r\n")
HTTP/1.1 301 Moved Permanently
Date: Tue, 06 Mar 2012 16:37:31 GMT
Location: http://twitter.com/
Before we do anything drastic, let's try telnet: zck@zck-desktop:~$ telnet twitter.com 80
Trying 199.59.150.7...
Connected to twitter.com.
Escape character is '^]'.
GET / HTTP/1.1
Host: twitter.com
HTTP/1.1 200 OK
Date: Wed, 07 Mar 2012 16:46:06 GMT
Again, cut for readability.So just sending those Maybe this is an arc thing. Let's try racket: > (define (write-message host message)
(let-values (((from-server to-server) (tcp-connect host 80)))
(display message to-server)
(close-output-port to-server)
(do ((response (read-line from-server) (read-line from-server)))
((eof-object? response) (lambda () nil))
(display response)
(newline))
(close-input-port from-server)))
> (write-message "google.com"
"GET / HTTP/1.1\r\n\r\nHost: google.com")
HTTP/1.1 200 OK
Date: Mon, 12 Mar 2012 15:12:04 GMT
Ok, so that works. Twitter? > (write-message "twitter.com"
"GET / HTTP/1.1\r\n\r\nHost: twitter.com")
>
Even though twitter prefers no www. on their urls: > (write-message "www.twitter.com"
"GET / HTTP/1.1\r\n\r\nHost: www.twitter.com")
>
So...nothing. Can anyone tell me what I'm forgetting? My money's on something that I'm not sending to them, or that I'm sending it badly escaped, or something. I can browse to twitter in Firefox just fine. |