Trying to write a very simple HTTP Client
Hi all, I've written the following program to connect to a submit an HTTP GET request to a server and print the response: module Main where import Network import System.IO main = withSocketsDo go go = do putStrLn "Connecting..." out <- connectTo "haskell.org" (PortNumber 80) hPutStrLn out "GET /\r" hPutStrLn out "Host: haskell.org\r" hPutStrLn out "\r" cs <- hGetLine out hClose out print cs When I run the program, however, I get the following error: HTTPClient: <socket: 1872>: hGetLine: end of file Can anyone see what's wrong? E.
On Fri, May 23, 2008 at 3:27 PM, Eric <eeoam@ukfsn.org> wrote:
Hi all,
I've written the following program to connect to a submit an HTTP GET request to a server and print the response:
module Main where
import Network import System.IO
main = withSocketsDo go
go = do putStrLn "Connecting..." out <- connectTo "haskell.org" (PortNumber 80) hPutStrLn out "GET /\r" hPutStrLn out "Host: haskell.org\r" hPutStrLn out "\r" cs <- hGetLine out hClose out print cs
When I run the program, however, I get the following error:
HTTPClient: <socket: 1872>: hGetLine: end of file
Can anyone see what's wrong?
I think you need to either print cs before you close the socket or make sure that cs is force (~computed) before you close the socket as laziness makes it get evaluated when you call print cs rather than when you call hGetLine. In other words, you try to read from the socket after you've closed it. Cheers, Johan
Hello Johan, Friday, May 23, 2008, 5:58:04 PM, you wrote:
cs <- hGetLine out hClose out print cs
I think you need to either print cs before you close the socket or make sure that cs is force (~computed) before you close the socket as laziness makes it get evaluated when you call print cs rather than when you call hGetLine. In other words, you try to read from the socket after you've closed it.
hGetLine, unlike hGetContents, has a strict semantics. actually, hGetContents is only lazy i/o operation, everything else (hGetChar, hGetBuf, hGetArray) is strict -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com
--- Eric <eeoam@ukfsn.org> wrote:
Hi all,
I've written the following program to connect to a submit an HTTP GET request to a server and print the response:
module Main where
import Network import System.IO
main = withSocketsDo go
go = do putStrLn "Connecting..." out <- connectTo "haskell.org" (PortNumber 80) hPutStrLn out "GET /\r" hPutStrLn out "Host: haskell.org\r" hPutStrLn out "\r" cs <- hGetLine out hClose out print cs
When I run the program, however, I get the following error:
HTTPClient: <socket: 1872>: hGetLine: end of file
Can anyone see what's wrong?
E.
Try calling 'hFlush out' prior to the call to hGetLine. I believe the output to the socket is buffered, so the receiver isn't seeing your GET request, and eventually closes the connection on its end, leading to the EOF on the hGetLine.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Robert Greayer wrote:
--- Eric <eeoam@ukfsn.org> wrote:
Hi all,
I've written the following program to connect to a submit an HTTP GET request to a server and print the response:
module Main where
import Network import System.IO
main = withSocketsDo go
go = do putStrLn "Connecting..." out <- connectTo "haskell.org" (PortNumber 80) hPutStrLn out "GET /\r" hPutStrLn out "Host: haskell.org\r" hPutStrLn out "\r" cs <- hGetLine out hClose out print cs
When I run the program, however, I get the following error:
HTTPClient: <socket: 1872>: hGetLine: end of file
Can anyone see what's wrong?
E.
Try calling 'hFlush out' prior to the call to hGetLine. I believe the output to the socket is buffered, so the receiver isn't seeing your GET request, and eventually closes the connection on its end, leading to the EOF on the hGetLine.
That works! Thank you! Eric M.
participants (4)
-
Bulat Ziganshin -
Eric -
Johan Tibell -
Robert Greayer