socket程序将文件发送到php

时间:2010-11-24 03:35:01

标签: php c apache sockets

昨天,我向php页面询问了有关套接字帖子文件数据的问题。

链接在这里

Upload and POST file to PHP page

当我按照方式行事时,我改变了我的代码。

char *str="POST /path/upload_file.php HTTP/1.0 \n Host: 00.00.00.00 \n   Content-Disposition: name=2.jpg;filename=2.jpg\r  " ;

write(sockfd, buf, filestat.st_size);
sprintf(send1,"%s%s\r\n",str,buf);
retval= send(sockfd,send1,sizeof(send1),0);

执行本程序时,可以

结果: 501方法未实施

未实施的方法

 到/index.html不受支持。

请求中的方法无效

<小时/> 位于localhost端口80的Apache / 1.3.39服务器

我想这是可能的: 1.阿帕奇不能支持什么?
     (我的apache不支持ssl) 2. http协议不详细? 3.其他?

非常感谢。

1 个答案:

答案 0 :(得分:1)

您的代码存在多个问题。

首先,Host标头是HTTP 1.1规范的一部分,不适用于HTTP 1.0。

其次,分隔标题和正文的分隔符为\r\n,而不是\n

第三,您在请求中使用Content-Disposition,它应该在响应中使用。

最后,您需要将请求发送为multipart/form-data。你联系到的答案是正确的。请求必须遵守该格式。

我不久前写过一个详细的例子(尽管在PHP中,但请求格式保持不变)。你可以find it here