我必须在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