阅读网页来源

时间:2012-07-01 08:21:32

标签: c xml windows

我有一个C ++应用程序,它读取页面源:

HINTERNET hInternet = InternetOpenA("InetURL/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
HINTERNET hConnection = InternetConnectA( hInternet, "ngg-l.com/Servers/getservers", 80, " "," ", INTERNET_SERVICE_HTTP, 0, 0 );
HINTERNET hData = HttpOpenRequestA( hConnection, "GET", "/", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0 );

char buf[ 2048 ] ;

HttpSendRequestA( hData, NULL, 0, NULL, 0 ) ;

DWORD bytesRead = 0 ;
DWORD totalBytesRead = 0 ;

while( InternetReadFile( hData, buf, 2000, &bytesRead ) && bytesRead != 0 )
{
    buf[ bytesRead ] = 0 ; // insert the null terminator.

    puts( buf ) ;          // print it to the screen.

    printf( "%d bytes read\n", bytesRead ) ;

    totalBytesRead += bytesRead ;
}

printf( "\n\n END -- %d bytes read\n", bytesRead ) ;
printf( "\n\n END -- %d TOTAL bytes read\n", totalBytesRead ) ;

InternetCloseHandle( hData ) ;
InternetCloseHandle( hConnection ) ;
InternetCloseHandle( hInternet ) ;

此代码适用于ngg-l.com,但在尝试获取ngg-l.com/servers的来源时,它会返回null。

0 个答案:

没有答案