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:
- POSTING MULTIPART/FORM-DATA USING .NET WEBREQUEST
- Upload files with HTTPWebrequest (multipart/form-data)
- How to recieve POST data sent using “application/octet-stream” in PHP?
- and PHP manual of course: Handling file uploads
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.
 
     
    