I am trying to write a simple console app that POSTs to a page and outputs the returned html to the console.
The code I have works, but it only returns part of the response. The only way I can figure out to make it work is to set the byte buffer to a size I know is big enough for the content being returned.
Is there anyway to check how big the buffer needs to be in order to receive the complete response?
Here is the code…
Uri uri = new Uri(@"http://bobssite/");
// Get the IPAddress of the website we are going to and create the EndPoint
IPAddress ipAddress = Dns.GetHostEntry(uri.Host).AddressList[0];
IPEndPoint endPoint = new IPEndPoint(ipAddress, 80);
// Create a new Socket instance and open the socket for communication
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
socket.Connect(endPoint);
// Attempt to send the request
int byteCount = 0;
try
{
string requestString =
"POST " + uri.PathAndQuery + " HTTP/1.1\r\n" +
"Host: " + uri.Host + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: 11\r\n" +
"\r\n" +
"user=bob";
byte[] bytesToSend = Encoding.ASCII.GetBytes(requestString);
byteCount = socket.Send(bytesToSend, SocketFlags.None);
}
catch (SocketException se)
{
Console.WriteLine(se.Message);
}
// Attempt to receive the response
if (byteCount > 0)
{
byte[] bytesReceived = new byte[256];
try
{
byteCount = socket.Receive(bytesReceived, SocketFlags.None);
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("HELP!! --> " + e.Message);
}
// Out the html we received
string html = Encoding.ASCII.GetString(bytesReceived);
Console.WriteLine(html);
}
else
{
Console.WriteLine("byteCount is zero!");
}
Console.Read();
I put the Receive call inside a loop to keep calling receive until the received bytes is zero and appended the data returned into a string variable.
Working Code….