IT学习联盟

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
IT学习联盟 门户 文章 C/C++/VC++/MFC/QT 查看内容

http协议使用实例

2012-8-18 02:45| 发布者: admin| 查看: 758| 评论: 0

#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;
 }
}
Archiver|IT学习联盟| 网站地图

Powered by Discuz! X2.5© 2001-2010 世界学习室 大伟制作

GMT+8, 2018-2-25 10:03 , Processed in 0.075559 second(s), 12 queries .