我必須在C中編寫一個連接到服務器的SSL客戶端,並獲取一個html或一個文件。我設法得到的HTML,但我不能下載二進制文件。例如,我試圖從https://www.openssl.org/source/openssl-1.0.0d.tar.gz下載一個3.8MB的文件,我的代碼只設法下載1.1mb的文件,我甚至不知道我是否能夠在其中獲得正確的數據。通過C中的SSL下載文件C
這裏是我爲它所做的功能:
char *sslReadfile (connection *c)
{
const int readSize = 1024;
char *rc = NULL;
int received, count = 0;
char buffer[1024];
char filename[40];
printf("Input the file name to be saved:\n");
scanf("%s",filename);
FILE *fp;
fp = fopen(filename, "wb");
if (c)
{
while (1)
{
if (!rc)
rc = malloc (readSize * sizeof (char) + 1);
else
rc = realloc (rc, readSize * sizeof (char) + 1);
received = SSL_read (c->sslHandle, buffer, readSize);
buffer[received] = '\0';
if (received > 0)
fprintf(fp,"%s",buffer);//strcat (rc, buffer);
if (received < readSize)
break;
//count++;
}
}
printf("\nFile saved!! %s !!!\n\n",filename);
fclose(fp);
return rc;
}
哦,我把它叫做這樣的:
char command[50];
sprintf(command,"GET /%s\r\n\r\n",relativepath);
sslWrite (c, command);
response = sslReadfile (c);
其中C是我的連接。
非常感謝。你解決了我的問題:)謝謝梅爾,下面的答案,我收到時不應該打破這個循環
2011-05-24 15:09:12