1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Example HTTP POST with C# and sockets

Discussion in 'C, C++, C#' started by armendarezcatin695, Jan 8, 2012.

  1. armendarezcatin695

    armendarezcatin695 Newbie

    Joined:
    Apr 25, 2010
    Messages:
    18
    Likes Received:
    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
     
  2. Gary Becks

    Gary Becks Power Member

    Joined:
    Apr 11, 2010
    Messages:
    675
    Likes Received:
    282
    Location:
    Atl
    Home Page:
  3. biglia

    biglia Regular Member

    Joined:
    Jun 28, 2008
    Messages:
    201
    Likes Received:
    37
  4. Chris22

    Chris22 Regular Member

    Joined:
    Sep 29, 2010
    Messages:
    400
    Likes Received:
    1,059
  5. armendarezcatin695

    armendarezcatin695 Newbie

    Joined:
    Apr 25, 2010
    Messages:
    18
    Likes Received:
    5
    biglia,

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

    catin
     
  6. armendarezcatin695

    armendarezcatin695 Newbie

    Joined:
    Apr 25, 2010
    Messages:
    18
    Likes Received:
    5
    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
     
  7. Gary Becks

    Gary Becks Power Member

    Joined:
    Apr 11, 2010
    Messages:
    675
    Likes Received:
    282
    Location:
    Atl
    Home Page:
    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.
     
    • Thanks Thanks x 1
  8. armendarezcatin695

    armendarezcatin695 Newbie

    Joined:
    Apr 25, 2010
    Messages:
    18
    Likes Received:
    5
    Thanks Gary... appreciate it...

    catin
     
  9. w84it

    w84it Regular Member

    Joined:
    Jun 18, 2009
    Messages:
    442
    Likes Received:
    752
    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
     
    • Thanks Thanks x 2
    Last edited: Jan 11, 2012
  10. armendarezcatin695

    armendarezcatin695 Newbie

    Joined:
    Apr 25, 2010
    Messages:
    18
    Likes Received:
    5
    Thanks w84it,

    That's what I was looking for...

    Regards,

    catin