InternetReadFile 返回损坏的响应

发布于 2024-08-14 05:38:25 字数 1186 浏览 2 评论 0原文

我正在执行 GET(REST) 并使用 InternetReadFile API 读取 xml 响应,当响应较小时,它工作正常,但当响应大于 10kb 时,InternetReadFile 返回中间带有垃圾字符的数据,或者它会截断数据的某些部分。当我由于存在垃圾字符或由于缺少某些部分而尝试重建响应时,生成的 xml 将被损坏。

如果我使用 fiddler 进行相同的 GET 调用,那么我会得到正确的响应。

这是代码片段

m_internetsession = InternetOpen("RestToolkit",INTERNET_OPEN_TYPE_PRECONFIG ,NULL,NULL,0);
if(m_internetsession == NULL)
{
    throw new exception ("InternetOpen call failed");
}
m_connection = InternetConnect(m_internetsession,m_uri.Gethost().c_str(),(INTERNET_PORT)m_uri.Getport(),"", "", INTERNET_SERVICE_HTTP, 0, 0);

HINTERNET request = HttpOpenRequest(m_connection,m_method.c_str(),m_uri.Getrelativepath().c_str(),NULL,NULL,NULL,0,0);

阅读回复:

 #define HTTP_BUFFER_LENGTH 1024
    if(response.empty())
    {
        CHAR szBuff[HTTP_BUFFER_LENGTH+1];
        memset(szBuff,0x00,sizeof(szBuff));
        DWORD bytesRead;
        while (InternetReadFile(request,szBuff, HTTP_BUFFER_LENGTH,&bytesRead) == TRUE && bytesRead > 0)
        {
            response.append(szBuff);
            memset(szBuff,0x00,sizeof(szBuff));
        }
    }

我做错了什么?

谢谢 杰尔

Am doing a GET(REST) and using InternetReadFile API to read response which is an xml, it works fine when the response is small but when the response is more then 10kb InternetReadFile returns data with junk characters in between or it chops some portion of data. When i try to reconstruct the response due to presence of junk character or due to lack of some portion the resulting xml would be corrupt.

If i make the same GET call using fiddler then am getting proper response.

Here is the code snippet

m_internetsession = InternetOpen("RestToolkit",INTERNET_OPEN_TYPE_PRECONFIG ,NULL,NULL,0);
if(m_internetsession == NULL)
{
    throw new exception ("InternetOpen call failed");
}
m_connection = InternetConnect(m_internetsession,m_uri.Gethost().c_str(),(INTERNET_PORT)m_uri.Getport(),"", "", INTERNET_SERVICE_HTTP, 0, 0);

HINTERNET request = HttpOpenRequest(m_connection,m_method.c_str(),m_uri.Getrelativepath().c_str(),NULL,NULL,NULL,0,0);

Read response:

 #define HTTP_BUFFER_LENGTH 1024
    if(response.empty())
    {
        CHAR szBuff[HTTP_BUFFER_LENGTH+1];
        memset(szBuff,0x00,sizeof(szBuff));
        DWORD bytesRead;
        while (InternetReadFile(request,szBuff, HTTP_BUFFER_LENGTH,&bytesRead) == TRUE && bytesRead > 0)
        {
            response.append(szBuff);
            memset(szBuff,0x00,sizeof(szBuff));
        }
    }

What am i doing wrong?

Thanks
JeeL

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怎言笑 2024-08-21 05:38:25

服务器可能在其回复中使用分块传输编码。你必须自己解析它(这并不难)。

编辑
我最初错过了它,但您的代码有一个错误:您试图将收到的内容解释为以空结尾的字符串,但事实并非如此。

response.append(szBuff);

需要改为

response.append(szBuff, szBuff + bytesRead);

The server, probably, uses chunked transfer encoding in its reply. You have to parse it yourself (which is not difficult at all).

EDIT:
I missed it initially, but your code has an error: you're trying to interpret received content as a null-terminated string, which it's not.

response.append(szBuff);

needs to be changed to

response.append(szBuff, szBuff + bytesRead);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文