tags:

views:

249

answers:

1

Hi, 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

A: 

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);
atzz
Thanks atzz, you were right server was using chunked transfer encoding. Thanks for highlighting the error :)
JeeZ
You're welcome! :)
atzz