【问题标题】:Random characters in WinInet outputWinInet 输出中的随机字符
【发布时间】:2015-04-07 12:07:53
【问题描述】:

我有以下代码:

#include <wininet.h>
#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <string.h>

using namespace std;


int main(int argc, char *argv[])
{
    HINTERNET connect = InternetOpen("MyBrowser",INTERNET_OPEN_TYPE_PRECONFIG,NULL, NULL, 0);

   if(!connect){
      cout<<"Connection Failed or Syntax error";
      return 0;
   }

HINTERNET OpenAddress = InternetOpenUrl(connect,"http://shahriar.byethost9.com/com2.html", NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE|INTERNET_FLAG_KEEP_CONNECTION, 0);

   if ( !OpenAddress )
   {
      DWORD ErrorNum = GetLastError();
      cout<<"Failed to open URL \nError No: "<<ErrorNum;
      InternetCloseHandle(connect);
      return 0;
   }

   char DataReceived[4096];
   DWORD NumberOfBytesRead = 0;
   while(InternetReadFile(OpenAddress, DataReceived, 4096, &NumberOfBytesRead) && NumberOfBytesRead )
   {
           cout << DataReceived;
   }

   InternetCloseHandle(OpenAddress);
   InternetCloseHandle(connect);

   cin.get();
    system("PAUSE");
    return EXIT_SUCCESS;
}

它抓取http://shahriar.byethost9.com/com2.html的源代码并打印到控制台。

问题是,我页面的源代码是&lt;h5&gt;paint&lt;/h5&gt;,但程序正在打印:&lt;h5&gt;paint&lt;/h5&gt;¥(每次编辑页面源时最后一个字符都不同)。

编辑我的页面源代码后,它变为:&lt;h5&gt;mspaint&lt;/h5&gt;,但程序再次打印旧源代码 (&lt;h5&gt;paint&lt;/h5&gt;¥)。

【问题讨论】:

  • 在使用缓冲区时不要假设缓冲区为空终止,或者自己对其进行空终止。你有字符串的实际长度。

标签: c++ session output wininet


【解决方案1】:

您是否尝试过以这种方式累积缓冲区:

cout << std::string(DataReceived, NumberOfBytesRead);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多