InternetReadFile 返回损坏的响应
我正在执行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
服务器可能在其回复中使用分块传输编码。你必须自己解析它(这并不难)。
编辑:
我最初错过了它,但您的代码有一个错误:您试图将收到的内容解释为以空结尾的字符串,但事实并非如此。
需要改为
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.
needs to be changed to