0

这是客户端代码(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;

这些代码都不起作用,全部打印为空。
有人可以帮忙吗?

4

2 回答 2

0

The following example illustrates "multipart/form-data" encoding. Suppose we have the following form:

 <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>

If the user enters "Larry" in the text input, and selects the text file "file1.txt", the user agent might send back the following data:

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--

If the user selected a second (image) file "file2.gif", the user agent might construct the parts as follows:

   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

于 2013-01-27T20:34:09.980 回答
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();
        }
于 2013-01-27T20:48:45.630 回答