简体   繁体   English

application / zip winsock c ++

[英]application/zip winsock c++

I'm trying to make a GET url request, response content type is "application/zip," but I'm having trouble outputting the content to an appropriate/readable zip file, here is my code, as you can tell, I'm trying to write out to 2 different files, neither are working, both are unreadable as zip files: 我正在尝试发出GET url请求,响应内容类型为“application / zip”,但我无法将内容输出到适当/可读的zip文件,这是我的代码,你可以告诉我,我我试图写出2个不同的文件,两个都没有工作,两个都不可读作为zip文件:

bool ionMyPlugin::downloadRact(int goodsId) {
    goodsId = 36028;
    WSADATA wsaData;
    SOCKET Socket;
    SOCKADDR_IN SockAddr;
    int lineCount = 0;
    int rowCount = 0;
    struct hostent *host;
    locale local;
    char buffer[10000];
    int i = 0;
    int nDataLength;
    string website_HTML;

    // website url
    string url = "eihome.eihoo.com";// / api ? mod = rayvr&app = apigoods&act = index&store_id = 34837 & secret_key = 8d410f7007b47e76c227cfa8c282c5f5&add_time = 1474877868 & company = eihome%E6%B5 % 8B % E8%AF % 95 % E4%BC % 81 % E4%B8 % 9A & sign = fd53cce56c4eb88e356d58f19c337e79";
    string url2 = "?mod=rayvr&app=apigoods&act=download&store_id=34837&goods_id=36010&secret_key=8d410f7007b47e76c227cfa8c282c5f5&add_time=1474877868&company=eihome%E6%B5%8B%E8%AF%95%E4%BC%81%E4%B8%9A&sign=fd53cce56c4eb88e356d58f19c337e79";// +goodsId;

    //HTTP GET
    string get_http = "GET /" + url2 + " HTTP/1.1\r\nHost: " + url + "\r\nConnection: close\r\n\r\n";

    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0){
        cout << "WSAStartup failed.\n";
        system("pause");
    }

    Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    host = gethostbyname(url.c_str());

    SockAddr.sin_port = htons(80);
    SockAddr.sin_family = AF_INET;
    SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);

    if (connect(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0){
        cout << "Could not connect";
        system("pause");
    }

    // send GET / HTTP
    send(Socket, get_http.c_str(), strlen(get_http.c_str()), 0);
    ofstream myfile;
    fstream myfile2("myfile2.zip", ios::out | ios::binary);
    myfile.open("example.zip", std::ios_base::binary);
    // recieve html
    int countWS = 0;
    while ((nDataLength = recv(Socket, buffer, 10000, 0)) > 0){
        int i = 0;
        while (buffer[i]){//buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r'){
            myfile << buffer[i];
            myfile2.write(reinterpret_cast<char *> (&buffer[i]), sizeof(buffer[i]));
            website_HTML += buffer[i];// >> 8);
            //website_HTML += ((buffer[i] << 8) >> 8);

            i += 1;
            if (i >= nDataLength)
                break;
        }
    }

    closesocket(Socket);
    WSACleanup();
    return false;
}

You may check this example : 您可以查看此示例:

#include <string>
#include <stdio.h>
#include <winsock2.h>
#define BUFFER_LEN (4096)
using std::string;
int main(int argc, char **argv)
{
    HANDLE fhand;
    string request;
    int sendret;
    int iRecv;
    int iResponseLength=0;
    int offset;
    DWORD dw;
    string res2;
    char recvBuffer[BUFFER_LEN]={0};
    string response;
    const char lb[]="\r\n\r\n";
    const char http[]="http\x3a//";
    const char snsn[]="%s\n";
    bool error1=false;
    bool error2=false;
    bool error3=false;
    int len3=strlen(argv[3]);
    printf(snsn,"\n-=[  httpget v1.0 by Avery Tarasov");
    printf(snsn,"-=[  Email: c0ldshadow@deeptide.com");
    printf(snsn,"-=[  Web: www.DeepTide.com");
    printf(snsn,"-=[  Dedicated to my fiance, Ostine!\n");
    printf(snsn,"Example usage: httpget theserver.com /somefolder/somefile.zip C:\\savehere.zip");
    if(argc!=4)
    {
        printf(snsn,"\nInvalid usage");
        goto cleanup;
    }
    WSADATA wsaData;
    if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0)
    {
        printf(snsn,"\nError initializing Winsock 2.2");
        goto cleanup;
    }
    error1=true;
    if(LOBYTE(wsaData.wVersion)!=2||HIBYTE(wsaData.wVersion)!=2)
    {
        printf(snsn,"\nWinsock 2.2 not available");
        goto cleanup;
    }
    printf(snsn,"\nWinsock 2.2 initialized via wsa2_32.dll");
    struct hostent *h;
    struct sockaddr_in sa;
    SOCKET server1;
    h=gethostbyname(argv[1]);
    if(h==0)
    {
        printf(snsn,"\ngethostbyname() failed");
        goto cleanup;
    }
    printf("%s","\nHost lookup succeeded for ");
    printf(snsn,argv[1]);
    memcpy((char *)&sa.sin_addr,(char *)h->h_addr,sizeof(sa.sin_addr));
    sa.sin_family=h->h_addrtype;
    sa.sin_port=htons(80);
    server1=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(server1==INVALID_SOCKET)
    {
        printf(snsn,"\nsocket() failed");
        goto cleanup;
    }
    error1=false;
    error2=true;
    if(connect(server1,(struct sockaddr *)&sa,sizeof(sa))<0)
    {
        printf(snsn,"\nconnect() failed");
        goto cleanup;
    }
    printf("%s","\nNow connected to ");
    printf("%s",argv[1]);
    printf(snsn," via port 80");
    request+="GET ";
    request+=argv[2];
    request+=" HTTP/1.0";
    request+=&lb[2];
    request+="Host: ";
    request+=argv[1];
    request+=lb;
    printf(snsn,"\nHTTP request constructed successfully:\n");
    printf(snsn,request.c_str());
    sendret=send(server1,request.c_str(),request.length(),0);
    if(sendret==-1)
    {
        printf(snsn,"send() failed");
        goto cleanup;
    }
    printf(snsn,"Successfully sent HTTP request to the server");
    printf(snsn,"\nWaiting for download to complete");
    fhand=CreateFile(argv[3],GENERIC_WRITE,FILE_SHARE_READ,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
    if(fhand==INVALID_HANDLE_VALUE)
    {
        printf(snsn,"\nCreateFile() failed");
        goto cleanup;
    }
    error2=false;
    error3=true;
    while((iRecv=recv(server1,recvBuffer,BUFFER_LEN-1,0))>0)
    {
        /*
        char hex[5];
        string packet;
        packet.reserve(5*iRecv);
        printf(snsn,"\n");
        printf("%s","Receiving ");
        printf("%d",iRecv);
        printf(snsn," byte packet:\n");
        for(int i=0;i<iRecv;++i)
        {
            wsprintf(hex,"%02x",(unsigned char)recvBuffer[i]);
            packet.append(hex);
            printf("%s ",hex);
        }
        */
        response.append(recvBuffer,iRecv);
        iResponseLength+=iRecv;
        ZeroMemory(recvBuffer,BUFFER_LEN);
    }
    if(iRecv==SOCKET_ERROR)
    {
        printf(snsn,"\n\nrecv() failed");
    }
    offset=response.find(lb)+4;
    if(offset!=string::npos)
    {
        printf("%s","\n\nFile starts at offset ");
        printf("%d\n",offset);
        printf(snsn,"\nInitial response from server:\n");
        for(int j=0;j<offset;++j)
        {
            printf("%c",response[j]);
        }
        res2.assign(response,offset,response.size());
        if(WriteFile(fhand,res2.data(),res2.size(),&dw,0)==0)
        {
            printf(snsn,"\nWriteFile() failed");
            goto cleanup;
        }
        else
        {
            printf("%s","\nFile successfully downloaded and saved to ");
            printf(snsn,argv[3]);
        }
    }
    cleanup:
    if(error1)
    {
        WSACleanup();
    }
    if(error2)
    {
        WSACleanup();
        closesocket(server1);
    }
    if(error3)
    {
        WSACleanup();
        closesocket(server1);
        CloseHandle(fhand);
    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM