Here is my scratch code:
using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace SocketsDemo
{
class Program
{
static void Main(string[] args)
{
Uri uri;
if (args.Any()) uri = new Uri(args[0]);
else uri = new Uri("http://odetocode.com/Articles/473.aspx"); //http://www.odetocode.com
var result = GetResource(uri);
Console.WriteLine(result);
Console.ReadLine();
}
private static string GetResource(Uri uri)
{
var host = uri.Host;
var resource = uri.PathAndQuery;
var hostEntry = Dns.GetHostEntry(host);
var socket = CreateSocket(hostEntry);
SendRequest(socket, host, resource);
return GetResponse(socket);
}
private static Socket CreateSocket(IPHostEntry hostEntry)
{
const int HTTP_PORT = 80;
var endPoint = new IPEndPoint(hostEntry.AddressList[0], HTTP_PORT);
var socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(endPoint);
return socket.Connected ? socket : null;
}
private static void SendRequest(Socket socket, string host, string resource)
{
var requestMessage = String.Format(
"GET {0} HTTP/1.1\r\n" +
"HOST: {1}\r\n" +
"\r\n", resource, host);
var requestBytes = Encoding.ASCII.GetBytes(requestMessage);
socket.Send(requestBytes);
}
private static string GetResponse(Socket socket)
{
int bytesCount = 0;
var buffer = new byte[256];
var result = new StringBuilder();
do
{
bytesCount = socket.Receive(buffer);
result.Append(Encoding.ASCII.GetString(buffer, 0, bytesCount));
} while (bytesCount > 0);
return result.ToString();
}
}
}
When i change HTTP part of request message in SendRequest
method to HTTP/1.0 - everything working.
But when I try to repeat this on HTTP/1.1, this block do { .. } while (bytesCount > 0)
hangs out on 181 cycle. Looks like server or client cannot deal with last chunk of bytes.
Can anybody explain what stays behind this and how i can repair this with smallest changes to existing code.