【问题标题】:Hep on HttpOpenRequest() and other Wininet functionsHep on HttpOpenRequest() 和其他 Wininet 函数
【发布时间】:2011-11-27 09:28:02
【问题描述】:

我仍然在从网络上保存随机网页。在对代码稍作修改后(以不同的方式调用 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/”时,我失败了,即程序运行不到一秒,没有输出文件。

这里有什么问题,是什么功能导致这种情况发生?

【问题讨论】:

  • 到底是什么失败了?当 Wininet 函数之一失败时,您应该调用 GetLastError,以了解正在发生的事情。
  • @Simón 这是 7 年前我忘了。

标签: c++ html wininet


【解决方案1】:

尝试从示例 URL 中删除“http://”。

Connection = InternetConnect(Initialize,"www.rottentomatoes.com", INTERNET_DEFAULT_HTTP_PORT,
    NULL,NULL,INTERNET_SERVICE_HTTP,0,0);

并确保 HttpOpenRequest 中的文件名有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 2021-02-26
    • 2017-04-09
    • 1970-01-01
    • 2023-03-17
    • 2023-03-15
    相关资源
    最近更新 更多