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:
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;
}