Like Tree2Likes
  • 1 Post By MK27
  • 1 Post By laserlight

HTTP POST in C Programming??!

This is a discussion on HTTP POST in C Programming??! within the Tech Board forums, part of the Community Boards category; hi, this is HTTP Post in c++ code: Code: sprintf(header, "POST /upload_ac.php HTTP/1.1\r\n"); sprintf(header, "%sHost: www.example.com\r\n", header); sprintf(header, "%sConnection: Keep-Alive\r\n", ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    34

    Exclamation HTTP POST in C Programming??!

    hi,

    this is HTTP Post in c++ code:
    Code:
    sprintf(header, "POST /upload_ac.php HTTP/1.1\r\n");
    sprintf(header, "%sHost: www.example.com\r\n", header);
    sprintf(header, "%sConnection: Keep-Alive\r\n", header);
    sprintf(header, "%sContent-Type: multipart/form-data; boundary=---------------------------90721038027008\r\n", header);
    sprintf(header, "%sContent-Length: %d\r\n", header, gr);
    sprintf(header, "%s\r\n", header);
    sprintf(header, "%s-----------------------------90721038027008\r\n", header);
    sprintf(header, "%sContent-Disposition: form-data; name=\"upfile\"; filename=\"%s\"\r\n", header, name.c_str());
    sprintf(header, "%sContent-Type: text/plain\r\n", header);
    sprintf(header, "%s\r\n", header);
    sprintf(header, "%s%s\r\n", header, to_send.c_str());
    sprintf(header, "%s-----------------------------90721038027008\r\n", header);
    sprintf(header, "%sContent-Disposition: form-data; name=\"name\"\r\n", header);
    sprintf(header, "%s\r\n", header);
    sprintf(header, "%supload\r\n\r\n", header);
    sprintf(header, "%s-----------------------------90721038027008--\r\n\r\n\0", header);
    i search google for similar code to post a file via web with C Programming, but i didn't find it

    i search for winsock2 with C Programming but difficult to find code that work

    help please

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    Well it's already (bad) C to start with, apart from name.c_str(), which you'll just need to convert from std::string into a char array (ditto for to_send)

    > sprintf(header, "%sHost: www.example.com\r\n", header);
    This kind of operation leads to undefined behaviour.
    Using header as both input and output in the same sprintf call is a bad idea.

    sprintf to a temporary, then strcat to where you want all the data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    34
    Quote Originally Posted by Salem View Post
    Well it's already (bad) C to start with, apart from name.c_str(), which you'll just need to convert from std::string into a char array (ditto for to_send)

    > sprintf(header, "%sHost: www.example.com\r\n", header);
    This kind of operation leads to undefined behaviour.
    Using header as both input and output in the same sprintf call is a bad idea.

    sprintf to a temporary, then strcat to where you want all the data.
    it's not c language.. it's c++ code and it's work
    am looking for c program code.. it's hard to find

    could you help me in it please? i need and example or tutorial
    hope if it with winsock2 not socket, win32 not linux
    thanx for reply soon

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    sprintf is C.

    Like I said, all you need to do is replace the c_str() things with a char pointer/array, and it will compile as C.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    That is atrocious. The person who wrote that did not understand what they were doing, or very likely, have any experience with either C or C++. Only a truly sick mind would think of using sprintf() that way. Horrible. Really, really bad code.

    The HTTP part is potentially correct, as long as you are sending a pure text file. Here's the RFC:

    RFC 2388 - Returning Values from Forms: multipart/form-data

    If you do some googling and reading on the nature of HTTP requests and specifically requests with a Content-Type of 'multipart/form-data', this is fairly straightforward. Using a tcp/ip socket is a separate issue, but there must be a million easy found tutorials, etc, on the topic for any platform.

    And as Salem said, that's mostly C. The parts you did not show may have more C++ in them -- who knows? Since you did not include the declaration of any of the variables, there's not much any one can do to help you with that.
    Last edited by MK27; 04-14-2012 at 07:09 AM.
    memcpy likes this.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    34
    ok this is full c++ code upload.cpp:
    Code:
    // upload.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include <windows.h>
    #include <fstream>
    #include <winsock2.h>
    #include <string>
    using namespace std;
    
    #define INFO_BUFFER_SIZE 32767
    
    //link libwsock32.a
    
    int getPcname(){
    TCHAR computerName[1024 + 1];
    char value;
    DWORD size = 1024;
    value = GetComputerName(computerName, &size);
    cout << value << endl;
    return value;
    }
    
    unsigned long WinsockStart()
    {
         WSADATA wsa;
         unsigned long ulong;
         struct hostent *host;
    
         if(WSAStartup(MAKEWORD(2,2), &wsa) < 0)
         {
              cout << "Error WinsockStart()" << endl;
              WSACleanup();
              return 1;
              }
    
         if((host=gethostbyname("www.example.com"))<0)
         {
              cout << "Fehler gethostbyname()" << endl;
              WSACleanup();
              return 2;
              }
    
         ulong = *(unsigned long*) host->h_addr;
    
         return ulong;
          }
    
         void error_exit(string text)
         {
         cout << text;
         WSACleanup();
         exit(EXIT_FAILURE);
          }
    
    
    
    int main()
    {
    DWORD i=0;
    DWORD  bufCharCount = INFO_BUFFER_SIZE;
    TCHAR  infoBuf[INFO_BUFFER_SIZE];
    SOCKET sock;
    struct sockaddr_in addr;
    unsigned long win=0;
    int con = 0, gr=0, send_r=0, rec=0;
    char header[2048], puffer[2018];
    string to_send="hello world";
    string name="test.txt";
    //char pcname2=getPcname();
    
    bufCharCount = INFO_BUFFER_SIZE;
      if( !GetComputerName( infoBuf, &bufCharCount ) )
    string pcname2=infoBuf;
    
    cout << infoBuf << endl;
    
    
    win=WinsockStart();
       if(win==1||win==2)
          error_exit("Error WinsockStart()");
    
    addr.sin_family=AF_INET;
    addr.sin_port=htons(80);
    addr.sin_addr.s_addr = win;
    
    sock = socket(AF_INET, SOCK_STREAM, 0);
       if(sock<0)
          error_exit("Error socket()");
    
    gr = (to_send.size()+name.size()+287);
    
    sprintf(header, "POST /upload_ac.php HTTP/1.1\r\n");
    sprintf(header, "%sHost: www.example.com\r\n", header);
    sprintf(header, "%sConnection: Keep-Alive\r\n", header);
    sprintf(header, "%sContent-Type: multipart/form-data; boundary=---------------------------90721038027008\r\n", header);
    sprintf(header, "%sContent-Length: %d\r\n", header, gr);
    sprintf(header, "%s\r\n", header);
    sprintf(header, "%s-----------------------------90721038027008\r\n", header);
    sprintf(header, "%sContent-Disposition: form-data; name=\"upfile\"; filename=\"%s\"\r\n", header, name.c_str());
    sprintf(header, "%sContent-Type: text/plain\r\n", header);
    sprintf(header, "%s\r\n", header);
    sprintf(header, "%s%s\r\n", header, to_send.c_str());
    sprintf(header, "%s-----------------------------90721038027008\r\n", header);
    sprintf(header, "%sContent-Disposition: form-data; name=\"name\"\r\n", header);
    sprintf(header, "%s\r\n", header);
    sprintf(header, "%supload\r\n\r\n", header);
    sprintf(header, "%s-----------------------------90721038027008--\r\n\r\n\0", header);
    
    sprintf(header, "%sContent-Disposition: form-data; name=\"pcname\"\r\n", header);
    sprintf(header, "%s\r\n", header);
    sprintf(header, "%sinfoBuf\r\n\r\n", header);
    sprintf(header, "%s-----------------------------90721038027008--\r\n\r\n\0", header);
    
    sprintf(header, "%sContent-Disposition: form-data; name=\"pcver\"\r\n", header);
    sprintf(header, "%s\r\n", header);
    sprintf(header, "%supload\r\n\r\n", header);
    sprintf(header, "%s-----------------------------90721038027008--\r\n\r\n\0", header);
    
    
    //sprintf(header, "%sContent-Disposition: form-data; name=\"api_sig\"\r\n\r\n", header);
    //wstrContent +=wstrSig;
    //wstrContent += L"\r\n";
    
    
    
    
    con = connect(sock, (SOCKADDR*)&addr, sizeof(addr));
       if(con < 0)
          error_exit("Error connect()");
    
    if(send_r=send(sock, header, strlen(header), 0)<0)
          error_exit("Error send()");
    
     while(rec=recv(sock, puffer, 2048, 0))
     {
      if(rec==0)
        error_exit("Server quit");
    
     printf("%s", puffer);
     }               
    
    closesocket(sock);
    WSACleanup();
    return EXIT_SUCCESS;
    }

    i want it in c programming

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    No, I'm not interested in doing your work for you (sent by PM)

    > the size of file is soo large it's 300KB..
    > i want it in c programming because it will compile in tinny size
    This is a load of rubbish.
    Try compiling the "release" version rather than the "debug" version, that will make it smaller.
    Try compiling with "dynamic" libraries rather than "static" libraries, that will make it smaller.

    If you want to spend days converting it to C (and adding loads of bugs as you go) for what will be a very minor benefit, then that's your choice.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    34
    ok.. i like it in c not c++ but i new in this language
    yes i prefer static library not dynamic.. only 1 standalone file

    what shall i do to do it in right way? i don't like to do it as am doing bugs.. i need the right way.. thanx

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by bakri
    i like it in c not c++ but i new in this language
    Then learn C and do it yourself. If you do not want to learn C, then perhaps "I like it in C" is just you fooling yourself.
    MK27 likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    So quit begging for us to convert your code for free. Either hire someone formally or learn to do it yourself.

    The way you're forming the header is actually awful, as it's inefficient and hard to read. You do know that you can copy more than one line of text, right?

    Your indentation makes it look like error_exit is part of the socket creation function.

    i don't like to do it as am doing bugs.. i need the right way.. thanx
    Sentences are delimited with punctuation and a beginning capital letter. Your rambling post full of strung-together ideas with ellipses is incredibly annoying to read.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by bakri View Post
    what shall i do to do it in right way? i need the right way..
    Then do it the right way. The code you posted is garbage and will not get you very far, altho it might work for a limited purpose.

    You need to figure out how multipart-form data works, and how to transmit on a tcp/ip socket, as I said before. If you have trouble with that, you can ask a specific question. If you don't have a specific question, then you have not put in any thought or effort and there is nothing to say.

    If you just want someone to write code for you, try https://www.elance.com/ or somewhere similar.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Registered User
    Join Date
    Mar 2012
    Posts
    34
    thanks to you all
    laserlight i don't like your rely it's hard enough to say that..

    i will learn it and be elite in it as PHP

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with HTTP POST
    By Anddos in forum Networking/Device Communication
    Replies: 5
    Last Post: 03-22-2009, 08:41 AM
  2. HTTP GET and POST REQUEST
    By strickey in forum Networking/Device Communication
    Replies: 3
    Last Post: 04-20-2007, 04:23 PM
  3. Http Post
    By X PaYnE X in forum C Programming
    Replies: 7
    Last Post: 04-17-2005, 04:20 AM
  4. HTTP Post Question
    By penney in forum C# Programming
    Replies: 2
    Last Post: 06-07-2004, 09:26 AM
  5. I Need A Example For Http Post
    By rjhome in forum C Programming
    Replies: 5
    Last Post: 02-17-2002, 09:49 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21