c++ - 了解 HttpOpenRequest() 和其他 Wininet 函数

标签 c++ html wininet

我仍在努力从网络上保存随机网页。在对代码稍加修改(以不同方式调用 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/

相关文章:

c++ - ffmpeg的libswscale中 "sw"是什么意思?

c++ - 为存档而构建的静态库,它不是被链接的体系结构 (x86_64)

html - 如何使表格变小并将表格放在中间

html - 为什么背景图像没有出现在我的 <div> 中?

c++ - WinInet CFtpConnection::openfile 多个文件

c++ - Hook InternetOpenUrl

c++ - OpenGL 索引数组

c++ - 清除输入缓冲区后 Stringstream 未提取

html - 为什么这个导航栏不居中?

c++ - 来自应用程序的 HTTP 请求在发件人的网络/代理和我们的网络主机之间消失