5

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).

8
  • 3
    Use the HttpWebRequest class.
    – SLaks
    Oct 17, 2011 at 20:05
  • 2
    @SLaks I'd go one further - WebClient... Oct 17, 2011 at 20:07
  • 1
    I need to create HTTP requst headers as a string, is it possible in these classes? Oct 17, 2011 at 20:11
  • Why do you need to create the HTTP request headers manually? Oct 17, 2011 at 20:19
  • you can add headers as a string, see my answer below
    – Tion
    Oct 17, 2011 at 20:24

4 Answers 4

7

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 ;)

2
  • Wow, i was like 5 hours on this problem and forgot to terminate request, thanks a lot! :) Oct 17, 2011 at 20:21
  • Now, your problem is solved, you have learned how create a Http header from scratch, It is time to switch to higher level of abstraction. As previously mentioned, use HttpWebRequest or WebClient
    – L.B
    Oct 17, 2011 at 20:29
1
var webClient = new WebClient();
webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
Stream responseStream = webClient.OpenRead("http://www.google.com");
if (responseStream != null)
{
   var responseReader = new StreamReader(responseStream);
   string response = responseReader.ReadToEnd();
   MessageBox.Show(response);
}
1

I found the Mentalis Proxy to be extremely helpful in understanding the Http Request/Response cycle at a socket level: http://www.mentalis.org/soft/projects/proxy/

0

There is the TcpClient class which on one hand allows you to have a full control over the request (you create request body as string) and on the other hand is much simpler to use than a low-level socket.

http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.