Created
April 27, 2017 05:28
-
-
Save lxfly2000/e1392e2fb92d60b1640570ae9af10f3d to your computer and use it in GitHub Desktop.
HTTP/HTTPS download demo using WinINet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//WinINet HTTP Get | |
//http://blog.csdn.net/wzx19840423/article/details/6536342 | |
//HTTPS: https://support.microsoft.com/zh-cn/kb/168151 | |
#include<iostream> | |
#include<Windows.h> | |
#include<WinInet.h> | |
#pragma comment(lib,"WinInet.lib") | |
int main() | |
{ | |
HINTERNET hOpen = InternetOpen(TEXT("KaikiUpdate"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);//打开连接,获得Internet句柄 | |
if (!hOpen)std::cout << "Error InternetOpen.\n"; | |
HINTERNET hConnect = InternetConnect(hOpen, TEXT("lxfly2000.github.io"), INTERNET_DEFAULT_HTTPS_PORT, | |
TEXT(""), TEXT(""), INTERNET_SERVICE_HTTP, 0, 0);//连接,获得连接句柄 | |
if (!hConnect)std::cout << "Error InternetConnect.\n"; | |
HINTERNET hReq = HttpOpenRequest(hConnect, TEXT("GET"), TEXT("/Kaiki/update.xml"), | |
HTTP_VERSION, TEXT(""), NULL, INTERNET_FLAG_SECURE, 0);//打开请求,获得请求句柄 | |
if (!hReq)std::cout << "Error HttpOpenRequest.\n"; | |
BOOL status = HttpSendRequest(hReq, NULL, 0, NULL, 0);//发送 | |
if (status == FALSE)std::cout << "Error HttpSendRequest.\n";//断网时在这里出错 | |
char szBuffer[1024] = ""; | |
DWORD dwByteRead = 0; | |
FILE *fp = NULL; | |
fopen_s(&fp, "dl.xml", "wb"); | |
do | |
{ | |
status = InternetReadFile(hReq, szBuffer, ARRAYSIZE(szBuffer), &dwByteRead); | |
if (status == FALSE)std::cout << "Error InternetReadFile.\n"; | |
std::cout << szBuffer; | |
fwrite(szBuffer, dwByteRead, 1, fp); | |
ZeroMemory(szBuffer, dwByteRead); | |
} while (dwByteRead); | |
fclose(fp); | |
InternetCloseHandle(hReq); | |
InternetCloseHandle(hConnect); | |
InternetCloseHandle(hOpen); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment