Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

c++ sockets, POST'ing


ghost's Avatar
0 0
#include "winsock2.h"
#include <iostream>
#include <fstream>
#include <windows.h>


using namespace std;
int main(int argc, char *argv[]) {
    
    // MessageBox(NULL, "hallo", "socketIndication", MB_OK);
    
    // Initialize Winsock.
    WSADATA wsaData;
    int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
    if ( iResult != NO_ERROR )
        printf("Error at WSAStartup()\n");

    // Create a socket.
    SOCKET m_socket;
    m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

    if ( m_socket == INVALID_SOCKET ) {
        printf( "Error at socket(): %ld\n", WSAGetLastError() );
        WSACleanup();
        return 1;
    }

    // Connect to a server.
    
    
    LPHOSTENT hostEntry;
    hostEntry = gethostbyname(argv[1]);	
	
    sockaddr_in clientService;
    clientService.sin_family = AF_INET;       
    clientService.sin_addr =  *((LPIN_ADDR)*hostEntry->h_addr_list); // inet_addr("127.0.0.1");
    clientService.sin_port = htons( 80 );

    if ( connect( m_socket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) {
        printf( "Failed to connect.\n" );
        WSACleanup();
        return 1;
    }

    // Send and receive data.    
    
    if(argc!=3)
    {
               cout<<"Usage:connect_web www.sau.no /index.html";
               return 1;
}

    char sendbuf[2000]="POST ";
    
    strcat(sendbuf, argv[2]);
    strcat(sendbuf, " HTTP/1.1\r\nHost: ");
    strcat(sendbuf, argv[1]);
    strcat(sendbuf, "\r\nUser-Agent: who_cares?\r\n");
    strcat(sendbuf, "Referer: google.com\r\n");
    strcat(sendbuf, "Connection: close\r\n");
    
    strcat(sendbuf, "Content-Type: application/x-www-form-urlencoded\r\n");
    strcat(sendbuf, "Content-Length: 17\r\n");
    
    strcat(sendbuf, "from=faen&submit=\r\n\r\n");
    // strcat(sendbuf, "submit=\r\n\r\n");
    
    
    cout<< sendbuf << "\n";
    char recvbuf[100];
    int bytesRecv;
    
    send( m_socket, sendbuf, strlen(sendbuf), 0 );
    
    
    while(bytesRecv!=SOCKET_ERROR)
    {
    bytesRecv=recv( m_socket, recvbuf, strlen(recvbuf), 0 );
    if(!bytesRecv)
    { break; }
    
     
    cout.write(recvbuf, strlen(recvbuf));
    }  
    
    WSACleanup();
    return 1;
     }

Why isnt it working? It should work.

EDIT: Damn smilies [edit Grind]No smileys[/edit]


ghost's Avatar
0 0

Hey, I had this prob when I was sending data with POST in PHP. You need the two \r\n 's after the CONTENT-LENGTH and NOT after the content. So the code would be:

....
strcat(sendbuf, "Content-Length: 17\r\n\r\n");
strcat(sendbuf, "from=faen&submit=\r\n\r\n");
....

I think that should work. BTW, you just check the 'disable smileys' checkbox when posting the message to disable the smileys like I am doing.

Thomas


ghost's Avatar
0 0

Thank you so much, you have no idea of how much time i've spent finding it out.