用Http Post上传C Wininet文件

时间:2012-04-10 22:12:31

标签: c winapi wininet

我正在尝试使用我的代码将PDF文件上传到我的PHP页面。但我只能上传一小部分内容。当我在Firefox中打开它时,它说PDF已损坏。另外,当我查看a的值时,它只是文件的一小部分。我怎么解决这个问题?

char *getmem();
int main(int argc, char *argv[]) 
{

char *a = getmem();
printf ("%s\n",a);
        HINTERNET hInet = InternetOpen(NULL, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
char frmdata[160000];
sprintf(frmdata,"-----------------------------acebdf13572468\r\nContent-Disposition: form-data; name=\"uploaded\"; filename=\"C:\\Users\\pen\\Desktop\\perltut.pdf\"\r\nContent-Type: application/pdf\r\n\r\n%s\r\n-----------------------------acebdf13572468--\r\n",a);

 char *hdrs = "Content-Type: multipart/form-data; boundary=---------------------------acebdf13572468\nUser-Agent: aa";
MessageBox(0,a,a,1);
        HINTERNET hConnect = InternetConnect(
                        hInet,
                        "host",
                        80,
                        NULL,
                        NULL,
                        INTERNET_SERVICE_HTTP,
                        0, 0
        );
LPCSTR accept[2]={"*/*", NULL};
        HINTERNET hRequest = HttpOpenRequest(
                hConnect,
                "POST",
                "/path",
                NULL, NULL, NULL, 0, 0
        );

        BOOL ret = HttpSendRequest(hRequest, hdrs,strlen(hdrs), frmdata, strlen(frmdata));





    system("pause");
    return 0;
}



char *getmem(){


FILE *fp;
long len;
char *buf;
fp=fopen("C:\\Users\\ff\\Desktop\\perltut.pdf","rb");
fseek(fp,0,SEEK_END); 
len=ftell(fp); 
fseek(fp,0,SEEK_SET); /
buf=(char *)malloc(len); 
fread(buf,len,1,fp); /
fclose(fp);
return buf;
}   

1 个答案:

答案 0 :(得分:0)

您的文件包含零字符,它被解释为字符串的结尾。您不能将sprintf用于二进制数据。