http协议使用实例
#include <stdio.h>
#include <windows.h>
#include <wininet.h>
#define MAXSIZE 1024
#pragma comment(lib, "Wininet.lib")
void urlopen(TCHAR*);
int main(int argc, TCHAR* argv[])
{
TCHAR ch[] = TEXT("http://dt.163.com/images/news/0605/news02053101_5.jpg");
urlopen(ch);
getchar();
return 0;
}
void urlopen(TCHAR* url)
{
HINTERNET hSession = InternetOpen(TEXT("UrlTest"), INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY, NULL, NULL, 0);
if(hSession != NULL)
{
HINTERNET hHttp = InternetOpenUrl(hSession, url, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0);
BYTE Temp[MAXSIZE];
ULONG Number = 1;
DWORD written;
HANDLE hFile;
if (hHttp != NULL)
{
wprintf_s(TEXT("%s\n"), url);
hFile = CreateFile( TEXT("ysl.jpg"),GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
if ( hFile == INVALID_HANDLE_VALUE )
{
goto hear;
}
while (Number > 0)
{
InternetReadFile(hHttp, Temp, MAXSIZE - 1, &Number);
Temp[Number] = '\0';
WriteFile( hFile,Temp,Number, &written,NULL);
}
InternetCloseHandle(hHttp);
hHttp = NULL;
}
hear:
CloseHandle( hFile );
InternetCloseHandle(hSession);
hSession = NULL;
}
}