我仍在努力从网络上保存随机网页。在对代码稍加修改(以不同方式调用 HttpOpenRequest())之后,程序成功下载了一个被重定向的页面。但是我仍然无法获取我想要的任何网页。
例子:
#include <windows.h>
#include <wininet.h>
#include <stdio.h>
#include <fstream>
#include <cstring>
#define SIZE 128
int main()
{
HINTERNET Initialize,Connection,File;
DWORD dwBytes;
char ch;
Initialize = InternetOpen("HTTPGET",INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
Connection = InternetConnect(Initialize,"http://www.rottentomatoes.com",INTERNET_DEFAULT_HTTP_PORT,
NULL,NULL,INTERNET_SERVICE_HTTP,0,0);
//File = HttpOpenRequest(Connection,NULL,"/index.html",NULL,NULL,NULL,0,0);
/**/
File = HttpOpenRequest(Connection,
"GET",
"/index.jsp",
"HTTP/1.1",
NULL, NULL,
INTERNET_FLAG_RELOAD | INTERNET_FLAG_EXISTING_CONNECT, 0);
if(HttpSendRequest(File,NULL,0,NULL,0))
{
std::ofstream webSource;
//webSource.open(strcat(argv[1], "__.html"));
webSource.open("a.html");
while(InternetReadFile(File,&ch,1,&dwBytes))
{
if(dwBytes != 1)break;
webSource << ch;
}
webSource.close();
}
InternetCloseHandle(File);
InternetCloseHandle(Connection);
InternetCloseHandle(Initialize);
return 0;
}
但是当我尝试下载“http://www.rottentomatoes.com/m/1209933-puss_in_boots/”时,我失败了,也就是说,程序运行不到一秒钟,并且没有产生任何输出文件。
这里有什么问题,是什么功能导致了这种情况发生?
最佳答案
尝试从示例 URL 中删除“http://”。
Connection = InternetConnect(Initialize,"www.rottentomatoes.com", INTERNET_DEFAULT_HTTP_PORT,
NULL,NULL,INTERNET_SERVICE_HTTP,0,0);
并确保 HttpOpenRequest 中的文件名有效。
关于c++ - 了解 HttpOpenRequest() 和其他 Wininet 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8284615/