문제

I am very confused because my code to to a GET request is working but it's giving me some extrange data.

The recv function is getting some rare bytes.

Here is the code:

send(Socket, request, strlen(request), 0);
    char *ptr = (char*)calloc(sizeof(char), RECV_LENGTH);
    char *ptr2 = NULL;
    size_t len_resp_total = 0;
    int nDataLength;
    int i = 0;
    while ((nDataLength = recv(Socket, ptr, RECV_LENGTH, 0)) > 0){
        printf("\n%s\n", ptr);
        if (i > 0){ //prepare in case that the response is bigger than RECV_LENGTH bytes
            len_resp_total += nDataLength;
            printf("%p apunta a %d len total: %d e i vale: %d\n", ptr2, *ptr2, len_resp_total, i);
            system("pause");
            ptr2 = (char*)realloc(ptr2, len_resp_total+1);
            if (ptr2==NULL){
                printf("Some error reallocating memory\n");
                return ERROR;
            }
            else{
                memcpy(ptr2 + len_resp_total - nDataLength, ptr, nDataLength);
                ptr2[len_resp_total] = '\0';
                i++;
            }   
        }
        else{
            len_resp_total += nDataLength;
            ptr2 = (char*)realloc(ptr2, len_resp_total);
            memcpy(ptr2, ptr, nDataLength);
            ptr2[len_resp_total] = '\0';
            i++;
        }
    }

I'm doing the GET request to this website: http://pastebin.com/raw.php?i=8Lq1aKGn

Here I pasted the response that I get: http://pastebin.com/RuhxKtXi. As you can see there is a strange value in lines 14, 50 and 69.

What is that?

도움이 되었습니까?

해결책

See in the pastebin where it says "Transfer-Encoding: chunked", this means the content is transmitted in chucks. The bits on line 14 50 and 69 are the hexidecimal values of the length of each chunk. Each chunk is that length, plus 2 characters (\r\n). Each size is specified in hexidecimal followed by \r\n.

It means there are 1e0d bytes, then ed5 bytes, then 0 bytes.

1e0d\r\n
[1e0d bytes of content here]\r\n
ed5\r\n
[ed5 bytes of content here]\r\n
0\r\n
[no content here]\r\n

see: http://en.wikipedia.org/wiki/Chunked_transfer_encoding

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top