winsock.h在c / c ++中发布请求

时间:2014-03-15 19:22:16

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

Q1: 我正在尝试使用c或c ++发送和发送http请求并获得响应 可以有人为我提供工作代码 或者是winsock的教程,所以我可以开始了

Q2: 是否有任何只支持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);
}

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

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