Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3458014
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:55:51+00:00 2026-05-18T09:55:51+00:00

I am trying to write a simple console app that POSTs to a page

  • 0

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();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:55:52+00:00Added an answer on May 18, 2026 at 9:55 am

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

    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];
        string page = "";
        try
        {
            while (byteCount > 0)
            {
                byteCount = socket.Receive(bytesReceived, SocketFlags.None);
                page += Encoding.ASCII.GetString(bytesReceived);
                Array.Clear(bytesReceived, 0, bytesReceived.Length);
            }
        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine("HELP!! --> " + e.Message);
        }
    
        // Out the html we received
        Console.WriteLine(page);
    }
    else
    {
        Console.WriteLine("byteCount is zero!");
    }
    
    Console.Read();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a console app that simply lists the number of
I'm trying to write a simple WPF app that has two ellipses, joined by
I'm trying to think of clever, clear, and simple ways to write code that
I am trying to write simple Visual Studio Add-In for code generation. In my
I am trying to write a really simple Outlook VSTO add in that checks
i'm trying to write a simple app with node.js to pull some json from
Hi trying to write a simple linq query from a tutorial I read. But
I'm trying to write a simple test that creates an image element, checks the
I have a simple console application that runs calculations in several threads (10-20 of
I'm trying to write a simple game/utility to calculate poker odds. I know there's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.