2

My .NET application sends a single simple text file to web-server over HttpWebRequest. But at the web-server side I am always getting an empty $_FILES array.

I read all this questions and articles:

Here is the test code:

public static void UploadFile()
{
    var boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");

    var httpRequest = (HttpWebRequest)WebRequest.Create(@"https://test.com/upload.php");
    httpRequest.Credentials = CredentialCache.DefaultNetworkCredentials;
    httpRequest.AllowAutoRedirect = true;
    httpRequest.MaximumAutomaticRedirections = 1;
    httpRequest.Method = "POST";
    httpRequest.ContentType = "multipart/form-data; boundary=" + boundary;
    httpRequest.KeepAlive = true;

    using (var requestStream = httpRequest.GetRequestStream())
    {
        var data = Encoding.UTF8.GetBytes("--" + boundary + "\r\n\r\n");
        requestStream.Write(data, 0, data.Length);

        data = Encoding.UTF8.GetBytes("Content-Disposition: form-data; name=\"MAX_FILE_SIZE\"\r\n\r\n");
        requestStream.Write(data, 0, data.Length);

        data = Encoding.UTF8.GetBytes("1048576"); // 1Mb
        requestStream.Write(data, 0, data.Length);

        data = Encoding.UTF8.GetBytes("--" + boundary + "\r\n");
        requestStream.Write(data, 0, data.Length);

        data = Encoding.UTF8.GetBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"upload.txt\"\r\n");
        requestStream.Write(data, 0, data.Length);

        data = Encoding.UTF8.GetBytes("Content-Type: application/octet-stream\r\n\r\n");
        requestStream.Write(data, 0, data.Length);

        data = File.ReadAllBytes(@"D:\upload.txt");
        requestStream.Write(data, 0, data.Length);

        data = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--");
        requestStream.Write(data, 0, data.Length);
    }

    using (var response = (HttpWebResponse)httpRequest.GetResponse())
    using (var sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
        Console.WriteLine(sr.ReadToEnd());
}

The /upload.php:

exit (var_dump($_FILES));

In console the output is always: array(0){}
Please, help.

Community
  • 1
  • 1
  • Have you checked the request and response with fiddler? Try uploading a file or multiple files manually, and then try to recreate the request with `HttpWebRequest`. – scheien Jun 11 '14 at 10:38
  • It solved the problem. Thank you very much! The Fiddler shows to me that the request body was not correct. I edited my code and now all works :). – user3447070 Jun 11 '14 at 11:17
  • Great, I'm glad to hear that you got it working :-) – scheien Jun 11 '14 at 11:33

1 Answers1

0

Writing up an answer according to comments.

Fiddler lets you inspect the data sent over the wire when making requests and retrieving responses. It is a proxy and it's free (for now) :-)

When one encounter problems like this one, it is essential to make it work manually, then try to recreate the request programmatically and inspect the request to see that it is built the correct way.

scheien
  • 2,457
  • 1
  • 19
  • 22