Use CHttpFile to correctly read data from the server.

Source: Internet
Author: User

Use CHttpFile to correctly read data from the server.

Some time ago, I found a problem when prompted the module for software upgrade, that is, the CHttpFile object cannot be used to get the correct response data length from the server, whether it is using the CHttpFile: QueryInfo method, still use the CHttpFile: GetLength method. The length obtained each time is not fixed, causing data read errors. The Code is as follows:

 

# Define READ_BUFFER_LENGTH 1024

# Define PAGE_BUFFER_SIZE 1024*4

 

Char * CHttpClient: ReadData (CHttpFile * pFile)

{

Char * pUtf8 = (char *) malloc (PAGE_BUFFER_SIZE );

UINT bufLen = PAGE_BUFFER_SIZE;

 

UINT recLen = 0;

UINT recSize = 0;

Char * pDest = pUtf8;

While (recLen = pFile-> Read (pDest, READ_BUFFER_LENGTH)> 0)

{

RecSize + = recLen;

If (recSize + READ_BUFFER_LENGTH> bufLen)

{

BufLen + = PAGE_BUFFER_SIZE;

PUtf8 = (char *) realloc (pUtf8, bufLen );

PDest = pUtf8 + recSize;

}

Else

{

PDest + = recLen;

}

}

PUtf8 [recSize] = '\ 0 ';

 

Return pUtf8;

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.