Example HTTP POST with C# and sockets

Joined
Apr 25, 2010
Messages
18
Reaction score
5
Does anyone have, (or know where I can find), a staightforward example of using System.Net.Sockets to do a 'POST'?

Any help or leads would be appreciated.

Regards,

catin
 
biglia,

Thanks for the effort but that is a 'webRequest' example vs a sockets example.

catin
 
Gary Becks,

Thanks to you too. That's a pretty cool ebook. However, I did a search though it and didn't see a post example for sockets.

Am I missing something?

Regaqrds,

catin
 
The very first example in the book uses sockets. So yes you missed something, especially if you read through it that fast. I would read through it again, slowly. Theres a lot to digest in that book.

Last but not least, you are going to have to learn to dig for info if you want to learn anything bro. Not trying to flame you or anything - but c# probably one of the most well documented languages there is so the info you are searching for is readily available if you take a sec to look for it. A simply search on amazon for C# sockets rendered quite a few good results http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias=aps&field-keywords=c#+sockets&x=0&y=0 I am sure google will have plenty more.

Good Luck.
 
Code:
try
            {

                LOG = "Requesting..";

                TcpClient client = new TcpClient(HOST, 80);
                NetworkStream stream = client.GetStream();




                List<string> HEADER = new List<string>();
                HEADER.Add(HTTP_COMMAND);

                HEADER.Add("Host: " + HOST);

                if (USER_AGENT.Length > 0)
                {
                    HEADER.Add("User-Agent: " + USER_AGENT);
                }

                if (ACCEPT.Length > 0)
                {
                    HEADER.Add("Accept: " + ACCEPT);
                }

                if (ACCEPT_ENCODING.Length > 0)
                {
                    HEADER.Add("Accept-Encoding: " + ACCEPT_ENCODING);
                }

                if (ACCEPT_CHARSET.Length > 0)
                {
                    HEADER.Add("Accept-Charset: " + ACCEPT_CHARSET);
                }

                if (ACCEPT_LANGUAGE.Length > 0)
                {
                    HEADER.Add("Accept-Language: " + ACCEPT_LANGUAGE);
                }


                string[] tmpExtra = Regex.Split(EXTRA_FIELDS, "\n");

                for (int z = 0; z < tmpExtra.Length; z++)
                {
                    if (tmpExtra[z].Trim().Length > 0)
                    {
                        HEADER.Add(tmpExtra[z]);
                    }
                }




                if (REFERER.Length > 0)
                {
                    HEADER.Add("Referer: " + REFERER);
                }

                if(COOKIES.Length > 0){
                    HEADER.Add("Cookies: " + COOKIES);
                }


                HEADER.Add("Connection: Keep-Alive");

                if (HTTP_COMMAND.Contains("POST") == true)
                {
                    CONTENT = "\r\n" + CONTENT;
                    HEADER.Add("Content-Type: " + CONTENT_TYPE);
                    HEADER.Add("Content-Length: " + CONTENT.Length);
                    HEADER.Add(CONTENT);
                }

                string dataToSend = "";
                for (int h = 0; h < HEADER.Count; h++)
                {
                    dataToSend = dataToSend + HEADER[h] + "\r\n";
                }
                dataToSend = dataToSend + "\r\n";

                byte[] send = Encoding.ASCII.GetBytes(dataToSend);
                stream.Write(send, 0, send.Length);
                stream.Flush();
                byte[] bytes = new byte[client.ReceiveBufferSize];
                int count = stream.Read(bytes, 0, (int)client.ReceiveBufferSize);
                String data = Encoding.ASCII.GetString(bytes);
                char[] unused = { (char)data[count] };
                LOG = data.TrimEnd(unused);
                stream.Close();
                client.Close();


                
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //  console("request '" + url + "' - " + ex.Message.ToString());
            }

Pretty tricky dealing with https/ssl I would suggest just going with the webrequest and cutting your work in half.
But there's some socket code for you to take a look
 
Last edited:
Back
Top