【问题标题】:winsock.h post request in c/c++ [closed]c/c++ 中的 winsock.h 发布请求 [关闭]
【发布时间】:2014-03-15 19:22:16
【问题描述】:

第一季度: 我正在尝试使用 c 或 c++ 发送和 http post 请求并获得响应 有人可以提供我的工作代码吗 或者winsock的教程,这样我就可以开始了

第二季度: 有没有只支持windows的c/c++网络库(我只针对windows)

提前致谢。

Edite1: 我得到了答案

#include <iostream>
#include <string>

#pragma comment (lib, "wsock32.lib")

#include <stdlib.h>
#include <winsock.h>
#include <sstream>


using namespace std;

#define BUFFERSIZE 1024
void die_with_error(char *errorMessage);
void die_with_wserror(char *errorMessage);

string HTTP_POST(string host,string path , string data){
    string request;
    string response;
    int resp_leng;

    char buffer[BUFFERSIZE];
    struct sockaddr_in serveraddr;
    int sock;

    WSADATA wsaData;
    char *ipaddress = "127.0.0.1";
    int port = 80;

    std::stringstream ss;
    ss << data.length();

    std::stringstream request2;

    request2 << "POST " << path << " HTTP/1.1"<<endl;
    request2 << "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)" << endl;
    //request2 << "" << endl;
    request2 << "Host: " << host << endl;
    request2 << "Content-Length: " << data.length() << endl;

    request2 << "Content-Type: application/x-www-form-urlencoded" << endl;
    request2 << "Accept-Language: en-au" << endl;
    request2 << endl;
    request2 << data;
    request = request2.str();
    cout<< request << endl << "###################################################" << endl << endl;
    //init winsock
    if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
        die_with_wserror("WSAStartup() failed");
    //open socket
    if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
        die_with_wserror("socket() failed");

    //connect
    memset(&serveraddr, 0, sizeof(serveraddr));
    serveraddr.sin_family      = AF_INET;
    serveraddr.sin_addr.s_addr = inet_addr(ipaddress);
    serveraddr.sin_port        = htons((unsigned short) port);
    if (connect(sock, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0)
        die_with_wserror("connect() failed");

    //send request
    if (send(sock, request.c_str(), request.length(), 0) != request.length())
        die_with_wserror("send() sent a different number of bytes than expected");

    //get response
    response = "";
    resp_leng= BUFFERSIZE;
    while (resp_leng == BUFFERSIZE)
    {
        resp_leng= recv(sock, (char*)&buffer, BUFFERSIZE, 0);
        if (resp_leng>0)
            response+= string(buffer).substr(0,resp_leng);
        //note: download lag is not handled in this code
    }

    //disconnect
    closesocket(sock);

    //cleanup
    WSACleanup();

    return  response;
}

int main(int argc, char *argv[])
{
    string  host = "localhost",
            path = "/page.php",
            data = "somedata=data1$somedata2=data2";
    string res = HTTP_POST(host,path,data);
    cout << res << endl;
    cin.get();
}

void die_with_error(char *errorMessage)
{
    cerr << errorMessage << endl;
    cin.get();
    exit(1);
}

void die_with_wserror(char *errorMessage)
{
    cerr << errorMessage << ": " << WSAGetLastError() << endl;
    cin.get();
    exit(1);
}

【问题讨论】:

标签: c++ c visual-studio networking winsock


【解决方案1】:

为此,我经常使用 curl。在这里你可以找到一堆 c++ 的例子 http://curl.haxx.se/libcurl/c/example.html

【讨论】:

    【解决方案2】:

    这两个都是 C(不是 C++)库,但它们可能有用:最流行的 libcURL 或不那么流行但由 W3C 赞助的 Libwww

    【讨论】:

    • tnx 我将使用 libcURL
    猜你喜欢
    • 2015-12-29
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2015-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多