HOWTO: 使用 InternetReadFile 获取文件
2008-10-28 09:49
393 查看
HOWTO: 使用 InternetReadFile 获取文件
察看本文应用于的产品查看机器翻译免责声明
文章编号 | : | 149413 |
最后修改 | : | 2002年5月17日 |
修订 | : | 1.0 |
概要
使用 WinInet API,InternetReadFile 返回下面的缓冲区:<body> <h1> 未找到 HTTP / 1.0 406 否可接受的对象 </h1> </body>更多信息
这是一个 HTTP 服务器特定的错误。 Microsoft IIS 可能会返回此错误存在时没有正确的 Accept 标头 ("接受: * / *") 在 HTTP 请求中。 可以使用下面的 API 添加这样的标头:InternetOpenUrl(), HttpSendRequest(), HttpAddRequestHeaders()注意其他 HTTP 服务器可能或不可能在相同的方式中的行为。 下面的代码演示如何传输任何类型的文件使用 WinInet API:
BOOL GetFile (HINTERNET IN hOpen, // Handle from InternetOpen() CHAR *szUrl, // Full URL CHAR *szFileName) // Local file name { DWORD dwSize; CHAR szHead[] = "Accept: */*/r/n/r/n"; VOID * szTemp[25]; HINTERNET hConnect; FILE * pFile; if ( !(hConnect = InternetOpenUrl ( hOpen, szUrl, szHead, lstrlen (szHead), INTERNET_FLAG_DONT_CACHE, 0))) { cerr << "Error !" << endl; return 0; } if ( !(pFile = fopen (szFileName, "wb" ) ) ) { cerr << "Error !" << endl; return FALSE; } do { // Keep coping in 25 bytes chunks, while file has any data left. // Note: bigger buffer will greatly improve performance. if (!InternetReadFile (hConnect, szTemp, 50, &dwSize) ) { fclose (pFile); cerr << "Error !" << endl; return FALSE; } if (!dwSize) break; // Condition of dwSize=0 indicate EOF. Stop. else fwrite(szTemp, sizeof (char), dwSize , pFile); } // do while (TRUE); fflush (pFile); fclose (pFile); return TRUE; }的相同的任务可以使用 WinInet MFC 类完成下面的方式:
#include <afx.h> #include <afxinet.h> BOOL GetFile (CHAR *szUrl, CHAR *szFileName) { TCHAR sz[1024]; CInternetSession session (_T("MyTest agent"), 1, INTERNET_OPEN_TYPE_DIRECT); CStdioFile* pFile = NULL; CHAR szHead[] = "Accept: */*/r/n/r/n"; DWORD nRead; CFile myFile; CFileException fileException; if ( !myFile.Open (szFileName, CFile::modeCreate | CFile::modeReadWrite, &fileException ) ) { cerr << "Can't open file: " << szFileName << " error = " << fileException.m_cause <<"/n"; return FALSE; } try { pFile = session.OpenURL (szUrl, 1, INTERNET_FLAG_RELOAD |INTERNET_FLAG_TRANSFER_BINARY, szHead, -1L); } catch (CInternetException *pEx) { cerr <<"OpenUrl failed: "<< pEx-> m_dwError << endl; return FALSE; } do { nRead = pFile->Read(sz, 1023); if (nRead != 0) myFile.Write (sz, nRead); } while (nRead != 0); myFile.Close(); pFile->Close(); if (pFile != NULL) delete pFile; session.Close(); return TRUE; }这篇文章中的信息适用于:
• | Microsoft Internet Explorer 3.0 |
• | Microsoft Internet Information Server 3.0 |
• | Microsoft Windows NT 3.51 Service Pack 5 |
• | Microsoft Windows NT 4.0 |
• | Microsoft Windows NT 4.0 Service Pack 4 |
• | Microsoft Windows 95 |
• | Microsoft Windows Internet Services (WinInet) |
相关文章推荐
- 使用 InternetReadFile 来获取文件
- 使用 InternetReadFile 来获取文件
- 使用wininet的InternetReadFile下载文件
- how to read data from web service ?(如何使用BMC Remedy Administrator 从Web Service获取数据?)
- 使用File Explorer向SD卡Push文件抛出:Failed to push selection: Read-only file sys
- 使用wininet的InternetReadFile下载文件
- How to get the file's icon using C++ - 用C++如何获取文件的图标
- <boost-03>如何使用boost filesystem获取给定子文件夹的目录?how to use boost filesystem to get subfolders?
- How to get the size of file in C++ - 在C++中如何获取文件的大小
- 获取文件类型使用 finfo_file 内置函数
- How to read a PCap file from Wireshark with C++
- How to read whole file? (C++)
- android--------根据文件路径使用File类获取文件相关信息
- 上传excel文件 并获取其信息(使用file上传组件)
- 如何使用SSH向IPOD Touch2传输文件 - How to SSH into iPod Touch 2G [Transfer Files, Apps via SSH Access]
- SQL Server 2005/2008 日志文件丢失后恢复数据库的办法(ldf lost/corrupt, how to restore/recover DB without LDF file)
- 使用Zend_Form_Element_File 获取文件
- 收藏——在C#使用SHGetFileInfo获取(管理)文件或者文件夹图标(C#封装Win32函数的一个例子)
- InternetOpen\InternetOpenUrl\InternetReadFile 等相关Win32 网络API 使用详细说明
- unable to read project file....不能读取项目文件的解决方法