C++ socket发送POST请求

// httpPost.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#include 
#include 
#include 
#pragma comment(lib, "Ws2_32.lib")

char* SendHttpRequest(char* weburl, char* url , char* detectorId ,char* JsonData);

int main(int argc, char* argv[])
{

    char* JsonData = "[{\"a\":\"b\"}]";
    char weburl[] = "www.test.com:8081";
    char* url = "/post";
    char* Id = "aaa";
    char* re = SendHttpRequest(weburl,url, Id, JsonData);
    printf("%s\r\n",re);

    system("pause");

    return 0;
}

char* SendHttpRequest(char* weburl, char* url , char* detectorId ,char* JsonData)
{
    time_t rawtime;
    struct tm * timeinfo;
    time_t t = time ( &rawtime );
    long timestamp = t;

    char *pHttpPost = "POST %s/%s/%ld HTTP/1.1\r\n"
                    "Content-Type: application/json\r\n"
                    "Content-Length: %d\r\n"
                    "Host:%s\r\n"
                    "Connection: Keep-Alive\r\n"
                    "User-Agent: Apache-HttpClient/4.5.3 (Java/1.7.0_75)\r\n"
                    "Expect: 100-continue\r\n"
                    "Accept-Encoding: gzip,deflate\r\n"
                    "\r\n%s";
    char strHttpPost[10240] = {0};  
    sprintf(strHttpPost, pHttpPost,url,detectorId, timestamp,strlen(JsonData),weburl,JsonData);  

    char* p = ":";
    char* web = strtok(weburl,p);
    int port = 80;
    char* ptr;
    if((ptr = strtok(NULL,p)))
    {
        port = atoi(ptr);
    }

    WORD wVersionRequested;  
    WSADATA wsaData;
    int err;

    wVersionRequested = MAKEWORD( 1, 1 );  

    err = WSAStartup(   wVersionRequested,   &wsaData   );  
    if( err != 0)   {  
        return false;  
    }  


    if( LOBYTE(wsaData.wVersion) != 1   ||  HIBYTE(   wsaData.wVersion   )   !=   1   )   {  
        WSACleanup(   );  
        return false;    
    }  

    SOCKET sockClient = socket(AF_INET,SOCK_STREAM,0);

    HOSTENT* thisHost;
    char* sIp = NULL;
    thisHost = gethostbyname(web);
    sIp = inet_ntoa(*(struct in_addr*)*thisHost->h_addr_list);

    SOCKADDR_IN addrSrv;
    addrSrv.sin_addr.S_un.S_addr = inet_addr(sIp);//本机地址,服务器在本机开启
    addrSrv.sin_family=AF_INET;
    addrSrv.sin_port=htons(port);// 设置端口号

    char* content = NULL;
    int a = connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));//连接服务器
    if(a == 0)
    {
        send(sockClient, strHttpPost, strlen(strHttpPost), 0);

        char recvBuf[1024];

        while(true)
        {
            int r = recv(sockClient,recvBuf,1024,0);//接受数据
            if(r > 0)
            {
                char* p = strstr(recvBuf,"HTTP/1.1 200");
                if(p != NULL)
                {
                    char* ptr = strstr(recvBuf,"\r\n\r\n");
                    if(ptr != NULL)
                    {
                        char* p_start = strtok(ptr,"{");
                        if(p_start != NULL)
                        {
                            char* p = strtok(NULL,"{");
                            if(p != NULL)
                                content = strtok(p,"}");
                        }
                    }

                    closesocket(sockClient);//关闭连接
                    return content;
                }

                p = strstr(recvBuf,"HTTP/1.1 400");
                if(p != NULL)
                {
                    break;
                }
            }
        }
        closesocket(sockClient);//关闭连接
    }
    return content; 
}

你可能感兴趣的:(vc++)