将文件错误上载到IIS7 + PHP Server

时间:2013-02-04 15:59:52

标签: c# php iis-7 webclient

我在Windows Server 2008 R2上使用PHP 5.4运行IIS7。我创建了一个带有SSL加密的网站,上传和下载文件的PHP脚本。我还使用Visual Studio 2012(C#)创建了一个客户端应用程序,该应用程序使用WebClient对象来执行上载/下载。下载工作完美。但是,当我尝试上传文件(使用WebClient)时,会发生WebException:

Message: The remote server returned an error: (500) Internal Server Error.
Status: System.Net.WebExceptionStatus.ProtocolError
InnerException: null;

PHP上传脚本目前为空(文件应上传到服务器上的临时目录,然后自动删除)。我应该补充一点,我可以在浏览器中运行PHP脚本而不会出现任何错误(即不上传任何内容)。起初我以为IIS服务帐户可能无法访问临时目录,但添加显式权限并不能解决问题。有什么想法吗?

我的C#代码如下:

public static void UploadFile(Uri uri)
{
    try
    {
        using (WebClient client = new WebClient())
        {
            // Ignore SSL warnings due to unsigned certificate.
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });

            client.Credentials = CredentialCache.DefaultNetworkCredentials;

            // Get a path/name of a new temporary file.
            String tempFile = Path.GetTempFileName();

            // Create a 1KB temporary file.
            using (var fileStream = new FileStream(tempFile, FileMode.Create, FileAccess.Write))
            {
                fileStream.SetLength(1024);

            }

            // Upload the file to the server.
            client.UploadFile(uri, tempFile);

            // Delete temporary file.
            File.Delete(tempFile);

        }

    }
    catch (WebException ex)
    {
        MessageBox.Show(ex.ToString());

    }

}

更新1 - IIS日志文件

2013-02-04 17:05:05 155.14.123.53 POST /Archive/uploadTest.php - 443 - 155.14.123.208 - 401 2 5 0
2013-02-04 17:05:05 155.14.123.53 POST /Archive/uploadTest.php - 443 HYPER\JOHNSD 155.14.123.208 - 500 0 0 15

1 个答案:

答案 0 :(得分:0)

哦,男孩......我真的觉得自己很蠢。 临时文件夹上的权限问题。我确实将IIS服务帐户添加到临时文件夹的安全设置,但我忘了添加权限。 = P