tags:

views:

1720

answers:

3

I am trying to write a "raw" HTTP client in C#. You may ask why??

My aim is to implement an HTTP client in J2ME (which can only do GET and limited POST), but first I need to understand the HTTP protocol better (hence the C# attempt).

My first attempts are failing:

var requestBytes = Encoding.UTF8.GetBytes(@"GET / HTTP/1.1
User-Agent: CSharp
Host: www.google.com

");
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect("www.google.com", 80);
socket.Send(requestBytes);
var responseBytes = new byte[socket.ReceiveBufferSize];
socket.Receive(responseBytes);
Console.Out.Write(Encoding.UTF8.GetString(responseBytes));

The socket opens, but then blocks at the Receive call. After a couple of seconds the remote host closes the connection.

Any ideas?

The same happens when I try to connect using the RAW mode in puTTY.

+3  A: 

It might be best if you're testing this thing, to install IIS or Apache locally and then use the address 127.0.0.1; it gives you more scope to test different things!

Being a bit of a prude, I wouldn't like it if somebody used my website to test their implementation of the HTTP protocol.

Phill
And to this point... Google could be saying... "user-agent: CSharp?... i think not!" (I don't think that's what's happening, but the point is, test in a controlled environment so that you *know* that the problem is your code.)
Timothy Khouri
sure, as a matter of fact, i was testing against my own server, but did not want to publish the address here. sorry google for using your address instead :|
Ries
A: 

I think you need to use TcpListener class.

// Begin listening for incoming connection requests
TcpListener myListener = new TcpListener("localhost", 8080); // change to yours
myListener.Start();

Then

Socket mySocket = myListener.AcceptSocket();
if (mySocket.Connected)
{
   // do some work
   mySocket.Send(<data>, <length>, 0);
}

Hope it will help.

sashaeve
He's making an outgoing connection, not listening for incoming ones.
Greg Beech
+1  A: 

If you're going to be playing down at the "raw" level, then you're responsible for understanding the protocols down there. See Hypertext Transfer Protocol -- HTTP/1.1 .

Otherwise, you should just stick to the WebRequest and WebClient classes.

John Saunders
Agreed - there's a lot more to the protocol than you can work out just by trying to make connections in a trial-and-error fashion. It's a serious undertaking to write an HTTP client.
Greg Beech
His point is to understand it from a code perspective so as to code it in another language... using .NET specific classes wouldn't be helpful in this case.
Timothy Khouri
Then he'd do well to read and understand that RFC and the ones it's based on. Can he even say what protocol HTTP is based on? (No helping the student, now)
John Saunders
Please say the reason for the downvote.
John Saunders
Like Timothy said, no use trying the .NET specific classes when I will be using Java in the end. Anyway, who said I have not read the RFC?
Ries
I saw no evidence of it. Maybe it's a difference in styles, but I'd have made my use of line end characters explicit. Tell me what protocol HTTP is based on, and I'll shut up about it.
John Saunders