SgDotNet
Singapore Professional .NET User Group -For Cool Developers

Post data to HTTP server

rated by 0 users
This post has 2 Replies | 1 Follower

Top 500 Contributor
Posts 6
tvraju Posted: 10-08-2005 6:37 PM

Hi

How to post data to  http server using vc++?

I can manage to post in vb.net.

Thanks

T.V. Raju Software Engineer Singapore Ph: +65 91544824 email: rajutv@hotmail.com
Top 500 Contributor
Posts 5
1. You can use MSXML2.ServerXMLHTTP object. (Check your VC++ on how to use COM/ActiveX)

2. Use winsock and implement post/get commands.


Here's a PHP sample but you should be able to translate it to winsock/vc++.




/* sendToHost
 * ~~~~~~~~~~
 * Params:
 *   $host - Just the hostname.  No http:// or /path/to/file.html portions
 *   $method - get or post, case-insensitive
 *   $path - The /path/to/file.html part
 *   $data - The query string, without initial question mark
 *   $useragent - If true, 'MSIE' will be sent as the User-Agent (optional)
 *
 * Examples:
 *   sendToHost('www.google.com','get','/search','q=php_imlib');
 *   sendToHost('www.example.com','post','/some_script.cgi',
 *              'param=First+Param&second=Second+param');
 */
function sendToHost($host,$method,$path,$data,$useragent=0)
{
    // Supply a default method of GET if the one passed was empty
    if (empty($method))
        $method = 'GET';
    $method = strtoupper($method);
    $fp = fsockopen($host,80);
    if ($method == 'GET')
        $path .= '?' . $data;
    fputs($fp, "$method $path HTTP/1.1\r\n");
    fputs($fp, "Host: $host\r\n");
    fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
    if ($method == 'POST')
        fputs($fp, "Content-length: " . strlen($data) . "\r\n");
    if ($useragent)
        fputs($fp, "User-Agent: MSIE\r\n");
    fputs($fp, "Connection: close\r\n\r\n");
    if ($method == 'POST')
        fputs($fp, $data);

    while (!feof($fp))
        $buf .= fgets($fp,128);
    fclose($fp);
    return $buf;
}

Top 50 Contributor
Posts 81
Gurus, correct me if i'm wrong here for the Winsock codings.  This is not an entire program but just a function.
I hope that it's easy for you to understand.  Actually, personally i find the above PHP code easier to understand...lolz
The below-mentioned code take memory buffer into consideration.

// SendHTTP: Main entry point for this code.
// url - The URL to GET/POST to/from.
// headers - Headers to be sent to the server.
// post - Data to be posted to the server, NULL if GET.
// postLength - Length of data to post.
// req - Contains the message and headers sent by the server.
// returns 1 on failure, 0 on success.

int SendHTTP(LPCSTR url,LPCSTR headers,BYTE *post,DWORD postLength,HTTPRequest *req) {
    WSADATA WsaData;
    SOCKADDR_IN sin;
    SOCKET sock;
    char buffer[1024];
    char protocol[20],host[128],request[2048];
    int l,port,chars,err;
    MemBuffer headersBuffer,messageBuffer;
    BOOL done;

    // Parse the URL
    ParseURL(url,protocol,sizeof(protocol)-1,host,sizeof(host)-1,request,sizeof(request)-1,&port);
    if(strcmp(protocol,"HTTP"))
        return 1;

    // Init Winsock
    err = WSAStartup (0x0101, &WsaData);
    if(err!=0)
        return 1;
    sock = socket (AF_INET, SOCK_STREAM, 0);
    if ( (unsigned int)socket == INVALID_SOCKET)
        return 1;
    // Connect to web sever
    sin.sin_family = AF_INET;
    sin.sin_port = htons( (unsigned short)port );
    sin.sin_addr.s_addr = GetHostAddress(host);
    if( connect (sock,(LPSOCKADDR)&sin, sizeof(SOCKADDR_IN) ) )
        return 1;
    // Send request
    if( !*request )
        lstrcpyn(request,"/",sizeof(request)-1);
    if( post == NULL )
        SendString(sock,"GET ");
    else SendString(sock,"POST ");
    SendString(sock,request);
    SendString(sock," HTTP/1.0\r\n");
    SendString(sock,"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel,
    application/msword, application/vnd.ms-powerpoint, */*\r\n");
    SendString(sock,"Accept-Language: en-us\r\n");
    SendString(sock,"Accept-Encoding: gzip, deflate\r\n");
    SendString(sock,"User-Agent: Mozilla/4.0\r\n");
    if(postLength) {
        wsprintf(buffer,"Content-Length: %ld\r\n",postLength);
        SendString(sock,buffer);
    }
    SendString(sock,"Host: ");
    SendString(sock,host);
    SendString(sock,"\r\n");
    if( (headers!=NULL) && *headers )
        SendString(sock,headers);

    // Send a blank line to signal end of HTTP headers
    SendString(sock,"\r\n");
    if( (post!=NULL) && postLength )
        send(sock,(char*)post,postLength,0);

    // Read the result
    // First read HTTP headers
    MemBufferCreate(&headersBuffer );
    chars = 0;
    done = FALSE;
   
    while(!done) {
        l = recv(sock,buffer,1,0);
        if(l<0)
            done=TRUE;
        switch(*buffer) {
            case '\r':
                break;
            case '\n':
                if(chars==0)
                    done = TRUE;
                    chars=0;
                    break;
            default:
                chars++;
                break;
        }
        MemBufferAddByte(&headersBuffer,*buffer);
    }
    req->headers=(char*)headersBuffer.buffer;
    *(headersBuffer.position) = 0;
   
    // Now read the HTTP body
    MemBufferCreate(&messageBuffer);
    do {
        l = recv(sock,buffer,sizeof(buffer)-1,0);
        if(l<0)
            break;
        *(buffer+l)=0;
        MemBufferAddBuffer(&messageBuffer,(BYTE*)buffer,l);
    } while(l>0);
    *messageBuffer.position = 0;
    req->message = (char*)messageBuffer.buffer;
    req->messageLength = (messageBuffer.position-messageBuffer.buffer);
   
    // Cleanup
    closesocket(sock);
    return 0;
}


thanks & regards e_har
Page 1 of 1 (3 items) | RSS
Copyright SgDotNet 2004-2008
Powered by Community Server (Commercial Edition), by Telligent Systems