double check your codes, it should works. someone has asked the same question before, but at last it is not related with WinInet.
解决方案 »
- 如何将数据放到指针指向的内存里面?
- 同样的方法为什么有问题
- 循环内int i,竟然提示重复申明
- 怎样把CString转换成char *
- 把map传进DLL中,为何不能使用itor++?
- 写一个程序判断计算机上的任何一个可执行的程序是“命令行”的还是“交互式”的,可行吗?
- 问题?
- 运行时产生的错误,小弟不知如何解决,请高手指教,在线等待。谢谢!!!
- 怎么用VC连接远程oracle数据库啊?急急急!!
- 怎么才能在视图中显示多张bmp啊?我想通过对话框打开的位图文件,打开新图后,以前的不消失。而是新图在它旁边显示。
- 请问:ADO中如何给Parameter的type赋一数组
- 有关AfxBeginThread错误的问题。
but, if i change the original file to another type ,for example, the original file is index.php, and when i change it to the file ,index.html,it works.
I don't know why.
can u give me some tips.
thx.
m_strPath = "/xxxx.php"; xxxx为你要的php文件名
HINTERNET hSession=::InternetOpen("Mozilla 4.0",INTERNET_OPEN_TYPE_PRECONFIG ,
"",INTERNET_INVALID_PORT_NUMBER,0); if(hSession == NULL){
AfxMessageBox("Session Initialization failed!",MB_OK,0);
VERIFY(::InternetCloseHandle(hSession));
return;
} HINTERNET hConnect=::InternetConnect(hSession,//当前internet会话句柄
m_strServer,//server name
INTERNET_INVALID_PORT_NUMBER,
NULL,//"",//user name
"",//password
INTERNET_SERVICE_HTTP,//Type of service to access
0,
0); if(hConnect == NULL){
AfxMessageBox("Connect Initialization failed!",MB_OK,0);
VERIFY(::InternetCloseHandle(hSession));
} LPCTSTR sType = "*/*";
LPCTSTR *pFileType = &sType;
HINTERNET hHttpFile=::HttpOpenRequest(hConnect,
"GET",
m_strPath,
"HTTP/1.1",
NULL,
pFileType,
INTERNET_FLAG_DONT_CACHE,
0); if(hHttpFile == NULL){
AfxMessageBox("HttpFile Initialization failed!",MB_OK,0);
VERIFY(::InternetCloseHandle(hConnect));
VERIFY(::InternetCloseHandle(hSession));
} BOOL bSendRequest = ::HttpSendRequest(hHttpFile,
NULL,
0,
0,
0); if(bSendRequest){
char chQueryBuf[16];
DWORD dwFileSize;
for(int i = 0; i < 16; i++)
chQueryBuf[i] = '\0';
dwFileSize = 0;
DWORD dwQueryBufLen = sizeof(chQueryBuf);
BOOL bQuery = ::HttpQueryInfo(hHttpFile,
HTTP_QUERY_CONTENT_LENGTH,
chQueryBuf,
&dwQueryBufLen,
NULL); if(bQuery){
dwFileSize = (DWORD)atol(chQueryBuf);
}
else{
dwFileSize = 10*1024;
}
char *lpszBuf = new char[dwFileSize + 1]; DWORD dwBytesRead;
BOOL bRead = ::InternetReadFile(hHttpFile,
lpszBuf,
dwFileSize+1,
&dwBytesRead);
char *lpszTemp = lpszBuf;
lpszTemp += dwFileSize;
*lpszTemp = '\0';
lpszTemp = NULL; CFile file;
if(file.Open("d:\\xxxx.php",CFile::modeCreate|CFile::modeWrite))
{
file.Write(lpszBuf,dwBytesRead);
int i;
i = strlen(lpszBuf);
void flush();
file.Close();
}
else
{
MessageBox("dlfj");
}
LPCTSTR *pFileType = &sType;
HINTERNET hHttpFile=::HttpOpenRequest(hConnect, "GET", m_strPath, "HTTP/1.1", NULL, pFileType, INTERNET_FLAG_DONT_CACHE,
0);
关键就是那个文件类型的参数LPCTSTR *lplpszAcceptTypes
我上面的程序是可以下载任意类型文件的,已测试通过