인터넷에서 파일을 다운로드 할 수있는 프로그램을 만들고 있습니다. 내가 파일을 다운로드하기 전에, 내가 파일 크기를 얻으려면, 그리고 난 InternetQueryDataAvailable을 시도하지만,인터넷에서 파일을 다운로드하면 HttpSendRequest가 오류 코드와 함께 실패합니다.
0 그럼 내가 hHttpRequest 시도의 값을 제공하지만, 그것은 오류 코드 ERROR_HTTP_HEADER_NOT_FOUND을 제공합니다. 그래서 HttpSendRequest를 추가했지만 오류 코드 ERROR_INTERNET_INVALID_URL을 제공합니다.
나는 시험장으로 평균 사이트를 사용하고 있습니다 : http://free.avg.com/us-en/download-free-all-product
파일을 다운로드 할 :
avg_free_x86_all_2014_4259a6848.exe 어떤 도움이 좋을 것, 감사합니다.코드 :
DWORD DownloadFile(PCHAR SaveDirectory)
{
HINTERNET hInternet;
CHAR StrBuffer[100];
hInternet = InternetOpen(InternetAgent, PRE_CONFIG_INTERNET_ACCESS, NULL, INTERNET_INVALID_PORT_NUMBER, 0);
if (hInternet != NULL)
{
CHAR TestUrl[] = "http://download.avgfree.com/filedir/inst";
CHAR TestFileName[] = "avg_free_x86_all_2014_4259a6848.exe";
HINTERNET hHttpSession = InternetConnect(hInternet, TestUrl, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
if (hHttpSession != NULL)
{
PCTSTR AcceptTypes[] = {"text/*", "application/exe", "application/zlib", "application/gzip", "application/applefile", NULL};
//I dont think application/exe is valid, but i could Not find a .exe format in the msdn list.
//http://www.iana.org/assignments/media-types/media-types.xhtml#application for the list
HINTERNET hHttpRequest = HttpOpenRequest(hHttpSession, "GET", TestFileName, "HTTP/1.1", TestUrl, &AcceptTypes[0],
INTERNET_FLAG_EXISTING_CONNECT|INTERNET_FLAG_RELOAD|INTERNET_FLAG_DONT_CACHE, 0);
if (hHttpRequest != NULL)
{
DWORD FileSize = 0;
DWORD BufferLength = sizeof(FileSize);
if (HttpSendRequest(hHttpRequest, NULL, 0, NULL, 0))
{
//See if HttpQueryInfo can get the file size.
if (HttpQueryInfo(hHttpRequest, HTTP_QUERY_CONTENT_LENGTH, &FileSize, &BufferLength, NULL))
{
sprintf_s(StrBuffer, sizeof(StrBuffer), "%u", FileSize);
MessageBox(MainWinHwnd, StrBuffer, "File Size", MB_OK);
}
else MessageBox(MainWinHwnd, "Failed to get the file size.", NULL, MB_OK);
//See if InternetQueryDataAvailable can get the file size.
if (InternetQueryDataAvailable(hHttpRequest, &FileSize, 0, 0))
{
sprintf_s(StrBuffer, sizeof(StrBuffer), "%u", FileSize);
MessageBox(MainWinHwnd, StrBuffer, "File Size", MB_OK);
}
else
{
MessageBox(MainWinHwnd, "Failed to get the file size.", NULL, MB_OK);
}
}
else
{
DWORD LastError = GetLastError();
sprintf_s(StrBuffer, sizeof(StrBuffer), "%u", LastError);
MessageBox(NULL, StrBuffer, NULL, MB_OK);
}
InternetCloseHandle(hHttpRequest);
}
else MessageBox(NULL, "Error #3", NULL, MB_OK);
InternetCloseHandle(hHttpSession);
}
else MessageBox(NULL, "Error #2", NULL, MB_OK);
InternetCloseHandle(hInternet);
}
else MessageBox(NULL, "Error #1", NULL, MB_OK);
return TRUE;
}
귀하의 빠르고 훌륭한 답변에 감사드립니다. HttpQueryInfo를 사용하여 파일 크기를 얻는 사람은 HTTP_QUERY_FLAG_NUMBER 플래그를 포함시켜야합니다. – AlwaysNub