1. 程式人生 > >C++使用socket往http地址傳送資料

C++使用socket往http地址傳送資料

//C++ POST方法 主體
int CDataBaseSink::request(char* hostname, char* api,DWORD port, char* parameters)
{      
    WSADATA WsaData;      
    WSAStartup(0x0101, &WsaData);        //初始化socket      
    struct hostent* host_addr = gethostbyname(hostname);    
    if (host_addr == NULL)     
    {         
        CTraceService::TraceString("host連線失敗!",TraceLevel_Warning);      
        return -103;    
    }        
    sockaddr_in sin;      
    sin.sin_family = AF_INET;      
    sin.sin_port = htons((unsigned short)port);      
    sin.sin_addr.s_addr = *((int*)*host_addr->h_addr_list);       
    int sock = socket(AF_INET, SOCK_STREAM, 0);  
    if (sock == -1) 
    {         
        return -100;     
    }        
    //建立連線      
    if (connect(sock, (const struct sockaddr *)&sin, sizeof(sockaddr_in) ) == -1)    
    {         
        CTraceService::TraceString("socket連線失敗!",TraceLevel_Warning);     
        return -101;    
    }       
    //初始化傳送資訊     
    char send_str[2048] = {0};        
    //頭資訊     
    strcat(send_str, "POST ");     
    strcat(send_str, api);      
    strcat(send_str, " HTTP/1.1\r\n");     
    strcat(send_str, "Host: ");     
    strcat(send_str, hostname);     
    strcat(send_str, "\r\n");      
    strcat(send_str, "Connection: keep-alive\r\n");        
    char content_header[100];      
    sprintf(content_header,"Content-Length: %d\r\n", strlen(parameters));       
    strcat(send_str, content_header);      strcat(send_str, "Cache-Control: max-age=0\r\n");     
    strcat(send_str, "Origin: http://www.baidu.com\r\n");      
    strcat(send_str, "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/15.0.849.0 Safari/535.1\r\n");     
    strcat(send_str, "Content-Type: application/x-www-form-urlencoded\r\n");     
    strcat(send_str, "Accept:  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");     
    strcat(send_str, "Referer: http://www.baidu.com/\r\n");     
    strcat(send_str, "Accept-Encoding: gzip,deflate,sdch\r\n");    
    strcat(send_str, "Accept-Language: zh-CN,zh;q=0.8\r\n");        
    //內容資訊      
    strcat(send_str, "\r\n");     
    strcat(send_str, parameters);       
    if (send(sock, send_str, strlen(send_str),0) == -1)     
    {          
        CTraceService::TraceString("send失敗!",TraceLevel_Warning);      
        return -101;    
    }       
    //獲取返回資訊      
    char recv_str[4096] = {0};      
    if (recv(sock, recv_str, sizeof(recv_str), 0) == -1)    
    {          
        CTraceService::TraceString("獲取返回引數失敗!",TraceLevel_Warning);     
        return -101;    
    }       
    CTraceService::TraceString(recv_str,TraceLevel_Normal);     
    WSACleanup(); 
    return 0;
}

呼叫例項:

//C++ POST方法 
/*引數:sParam   :"receipt_data=zhz&b=2";  
           HostIp:"0.0.0.0"
            Port : 3306
            ApiUrl:"http:ip:port/mothd/....."
 */
std::string CDataBaseSink::HttpPostparams(TCHAR* HostIp,TCHAR* ApiUrl,DWORD Port,TCHAR* sParam)
{
       
    Json::Value root;
    Json::FastWriter writer;
    CString parammsg;
    int errcode = request(HostIp,ApiUrl,Port,sParam); 
    parammsg.Format("(方法:HttpPostparams)Http-POST資訊:errcode: '%d'; data: '%s'",errcode,sParam);
    CTraceService::TraceString(parammsg,TraceLevel_Normal);

    root["result_out"] =0;
    return writer.write(root);
}