PHP无法获取作为multipart / form-data发送的原始数据

时间:2013-01-27 19:49:14

标签: c# php

这是客户端代码(C#):

string boundary = "--ABC";

try
{
    WebRequest request = WebRequest.Create(url);
    request.Method = "POST";                
    request.ContentType = "multipart/form-data;boundary="+boundary;


    using (var requestStream = request.GetRequestStream())
    using (var writer = new StreamWriter(requestStream))
    {
        writer.WriteLine(boundary);
        writer.WriteLine("Content-Disposition: form-data; name=\"data\"");
        writer.WriteLine();
        writer.WriteLine("abcdefg");
        writer.WriteLine(boundary + "--");
        writer.Flush();
    }
    string responseData = string.Empty;
    using (var response = request.GetResponse())
    using (var responseStream = response.GetResponseStream())
    using (var reader = new StreamReader(responseStream))
    {
        responseData=reader.ReadToEnd();
    }

这是服务器代码(php):

print file_get_contents("php://input");

或:

print $_POST["data"];

或:

print $http_raw_post_data;

或:

$fp=fopen("php://output","rb");
$contents=fread($fp,5);
fclose($fp);
print $contents;

这些代码不起作用,所有打印都是空的。
有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

以下示例说明了“multipart / form-data”编码。假设我们有以下形式:

 <FORM action="http://server.com/cgi/handle"
       enctype="multipart/form-data"
       method="post">
   <P>
   What is your name? <INPUT type="text" name="submit-name"><BR>
   What files are you sending? <INPUT type="file" name="files"><BR>
   <INPUT type="submit" value="Send"> <INPUT type="reset">
 </FORM>

如果用户在文本输入中输入“Larry”,并选择文本文件“file1.txt”,则用户代理可能会发回以下数据:

Content-Type: multipart/form-data; boundary=AaB03x

   --AaB03x
   Content-Disposition: form-data; name="submit-name"

   Larry
   --AaB03x
   Content-Disposition: form-data; name="files"; filename="file1.txt"
   Content-Type: text/plain

   ... contents of file1.txt ...
   --AaB03x--

如果用户选择了第二个(图像)文件“file2.gif”,则用户代理可以按如下方式构建部件:

   Content-Type: multipart/form-data; boundary=AaB03x

   --AaB03x
   Content-Disposition: form-data; name="submit-name"

   Larry
   --AaB03x
   Content-Disposition: form-data; name="files"
   Content-Type: multipart/mixed; boundary=BbC04y

   --BbC04y
   Content-Disposition: file; filename="file1.txt"
   Content-Type: text/plain

   ... contents of file1.txt ...
   --BbC04y
   Content-Disposition: file; filename="file2.gif"
   Content-Type: image/gif
   Content-Transfer-Encoding: binary

   ...contents of file2.gif...
   --BbC04y--
   --AaB03x--

Multipart Form Post in C# Example

答案 1 :(得分:0)

尝试:

 string boundary = "AaB03x";

    try
    {
        WebRequest request = WebRequest.Create(url);
        request.Method = "POST";                
        request.ContentType = "multipart/form-data;boundary="+boundary;

        using (var requestStream = request.GetRequestStream())
        using (var writer = new StreamWriter(requestStream))
        {
            writer.WriteLine("--"+boundary);
            writer.WriteLine( "Content-Disposition: form-data; name=\"files\"; filename=\"file1.txt\"");
             writer.WriteLine("Content-Type: text/plain
    ");
            writer.WriteLine("example");        
            writer.WriteLine("--"boundary + "--");
            writer.Flush();
        }
        string responseData = string.Empty;
        using (var response = request.GetResponse())
        using (var responseStream = response.GetResponseStream())
        using (var reader = new StreamReader(responseStream))
        {
            responseData=reader.ReadToEnd();
        }