📄 ce_http.cpp
字号:
#include "Wininet.h"
#include <initguid.h>
#include <connmgr.h>
#include <connmgr_proxy.h>
void HttpDownload(TCHAR * szServer, TCHAR * szObject)
{
HINTERNET hConnect = InternetConnect(hSession,
szServer,
INTERNET_INVALID_PORT_NUMBER,
L"",
L"",
INTERNET_SERVICE_HTTP,
0,
0);
HINTERNET hHttpFile = HttpOpenRequest(hConnect,
L"GET",
szObject,
HTTP_VERSION,
NULL,
0,
INTERNET_FLAG_DONT_CACHE,
0) ;
bool bSendRequest = HttpSendRequest(hHttpFile, NULL, 0, 0, 0);
if(!bSendRequest)
{
InternetCloseHandle(hHttpFile);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
delete [] pBufQuery;
return NULL;
}
memset(pBufQuery, 0, 1024*sizeof(TCHAR));
dwLengthBufQuery = 1024*sizeof(TCHAR);
bQuery = HttpQueryInfo(hHttpFile,
HTTP_QUERY_CONTENT_LENGTH,
pBufQuery,
&dwLengthBufQuery,
NULL) ;
DWORD dwFileLen = _wtoi(pBufQuery);//得到下载的大小
delete [] pBufQuery;
if(dwFileLen <= 0)
{
InternetCloseHandle(hHttpFile);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
return NULL;
}
char * pRead = new char[dwFileLen+1];
memset(pRead, 0, dwFileLen+1);
DWORD dwRead;
BOOL bRet = InternetReadFile(hHttpFile, pRead, dwFileLen, &dwRead);
TCHAR * pContent = new TCHAR[dwFileLen+1];
memset(pContent, 0, (dwFileLen+1)*2);
MultiByteToWideChar(CP_UTF8, 0, pRead, strlen(pRead), pContent, dwFileLen);//全部转化成Unicode码处理
//此处可对pContent进行操作
delete []pRead;
delete []pContent;
InternetCloseHandle(hHttpFile);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -