Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP over C# sockets

I am trying to send HTTP request and recieve responce from the server over C# sockets, and i'm new with this language.

I've wrote following code (IP resolved correctly):

IPEndPoint RHost = new IPEndPoint(IP, Port);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(RHost);

String HTTPRequestHeaders_String = "GET ?q=fdgdfg HTTP/1.0
Host: google.com
Keep-Alive: 300
Connection: Keep-Alive
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.205 Safari/534.16
Referer: http://google.com/";

MessageBox.Show(HTTPRequestHeaders_String, "Request");

byte[] HTTPRequestHeaders = System.Text.Encoding.ASCII.GetBytes(HTTPRequestHeaders_String);
socket.Send(HTTPRequestHeaders, SocketFlags.None);

String Response = "";
byte[] buffer = new byte[(int) socket.ReceiveBufferSize];

int bytes;
do
{
    // On this lane program stops to react
    bytes = socket.Receive(buffer);
    // This line cannot be reached, tested with breakpoint
    Response += Encoding.ASCII.GetString(buffer, 0, bytes);
}
while (bytes >= 0);

MessageBox.Show(Response, "Response");

What am i doing wrong? I need just to load full HTML of page, or at least few characters from response (i cant do even this).

like image 480
Andrew Dryga Avatar asked Jan 19 '23 11:01

Andrew Dryga


1 Answers

I would suggest looking into the protocol itself if you want to do this raw, http://www.w3.org/Protocols/HTTP/1.0/spec.html#Request

And try sending the CRLF to terminate the request ;)

like image 149
Sabre Avatar answered Jan 28 '23 00:01

Sabre