Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder4.news.weretis.net!newsfeed.fsmpi.rwth-aachen.de!uucp.gnuu.de!xts.gnuu.de!.POSTED!not-for-mail From: Simon Krahnke Newsgroups: comp.lang.ruby Subject: Re: Common way to read/write to/from TCPSocket Date: Sat, 06 Apr 2013 03:49:12 +0200 Organization: there's no z in organisation Lines: 42 Message-ID: <871uaoe8dj.fsf@xts.gnuu.de> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: xts.gnuu.de 1365212952 15863 172.19.115.2 (6 Apr 2013 01:49:12 GMT) X-Complaints-To: usenet@nowhere.invalid User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux) Cancel-Lock: sha1:bo0m+S5aXZb+Jar+4TDPW192du0= Xref: csiph.com comp.lang.ruby:6782 * yspro <2ys.pro@gmail.com> (2013-03-29) schrieb: >Hi there! I want to know common way to create TCPSocket, write http request and get response. > >My simple function: > >def read_from_sock(host, port) > sock = TCPSocket.new(host, port) > > request = '' > request << "GET / HTTP/1.1\r\n" > request << "Host: #{host}\r\n" > request << "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1\r\n" > request << "\r\n" > > sock.write(request) > response = '' > > until sock.eof? > response << sock.read > end > > sock.close > response >end > > > >read_from_sock('yahoo.com', 80) returns > >"HTTP/1.1 301 Redirect\r\nDate: Fri, 29 Mar 2013 06:56:20 GMT\r\nConnection: close\r\nServer: YTS/1.20.13\r\nCache-Control: no-store\r\nContent-Type: text/html\r\nContent-Language: en\r\nLocation: http://www.yahoo.com/\r\nContent-Length: 211\r\n\r\nRedirect\n\n\n \"http://www.yahoo.com/\".

\n\n\n\n\u0000" > >read_from_sock('facebook.com', 80) or read_from_sock('google.com', 80) hangs forever Your code works with me after i applied Robert's changes and I commented out the User-Agent line. They all (haven't tried facebook) redirect with 301 code, there's nothing wrong with that. If you plan to do a full http-client than good luck, its quite a lot of work. mfg, simon .... l