- #pragma comment(lib, "ws2_32.lib")
- //远程IP,port,要下载的文件的绝对路径,本地保存的绝对路径
- bool GetHTTP(string sRemoteIP,u_short nRemotePort,string sRemoteFilePath,string sSavePath)
- {
- try
- {
- WORD wVersionRequested = MAKEWORD(1,1);
- WSADATA wsaData;
- int nRet;
- nRet = WSAStartup(wVersionRequested, &wsaData);
- if (nRet)
- {
- char ermsg[1024]={0};
- sprintf_s(ermsg,"download %s,WSAStartup() error,error code : %d",sRemoteFilePath, nRet);
- throw ermsg;
- }
- if (wsaData.wVersion != wVersionRequested)
- throw "WinSock version not supported";
- SOCKET Socket;
- Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (Socket == INVALID_SOCKET)
- {
- char ermsg[1024]={0};
- sprintf_s(ermsg,"download %s,socket() error,error code : %d", sRemoteFilePath,WSAGetLastError());
- throw ermsg;
- }
- // Find the port number for the HTTP service on TCP
- SOCKADDR_IN saServer;
- saServer.sin_port = htons(nRemotePort);
- saServer.sin_family = AF_INET;
- saServer.sin_addr.s_addr = inet_addr(sRemoteIP.c_str());
- nRet = connect(Socket, (LPSOCKADDR)&saServer, sizeof(SOCKADDR_IN));
- if (nRet == SOCKET_ERROR)
- {
- char ermsg[1024]={0};
- sprintf_s(ermsg,"download %s,connect() error,error code : %d",sRemoteFilePath, WSAGetLastError());
- closesocket(Socket);
- throw ermsg;
- }
- // Format the HTTP request
- char szBuffer[102400];
- sprintf_s(szBuffer, "GET %s\n", sRemoteFilePath.c_str());
- nRet = send(Socket, szBuffer, strlen(szBuffer), 0);
- if (nRet == SOCKET_ERROR)
- {
- char ermsg[1024]={0};
- sprintf_s(ermsg,"download %s,send() error,error code : %d", sRemoteFilePath,WSAGetLastError());
- closesocket(Socket);
- throw ermsg;
- }
- FILE *fp ;
- int nError = fopen_s(&fp,sSavePath.c_str(),"wb");
- if (nError)
- {
- char ermsg[10240]={0};
- sprintf_s(ermsg,"save %s file error.", sSavePath.c_str());
- closesocket(Socket);
- throw ermsg;
- }
- while(1)
- {
- nRet = recv(Socket, szBuffer, sizeof(szBuffer), 0);
- if (nRet == SOCKET_ERROR)
- {
- char ermsg[1024]={0};
- sprintf_s(ermsg,"download %s,recv() error,error code : %d",sRemoteFilePath, WSAGetLastError());
- closesocket(Socket);
- throw ermsg;
- }
- if (nRet == 0)
- break;
- fwrite(szBuffer, nRet, 1, fp);
- }
- fclose(fp);
- closesocket(Socket);
- WSACleanup();
- }
- catch ( char* sErrMsg)
- {
- Logging tLog;
- tLog.InitLog();
- tLog.WriteLog(ERRLOG, true, sErrMsg);
- WSACleanup();
- return false;
- }
- return true;
- }