繁体   English   中英

远程服务器返回错误:(405)方法不允许

[英]The remote server returned an error: (405) Method Not Allowed

我想使用HttpWebRequest将文件发布到服务器:

private void testUpload()
{
    FileStream source = File.Open(@"C:\test.txt", FileMode.Open);

    var request = 
    (HttpWebRequest)WebRequest.Create(new Uri("http://example.com/Project/"));
    request.Method = "POST";

   request.BeginGetResponse(DataUploadCompleted, request);
}

private void DataUploadCompleted(IAsyncResult ar)
{
    var request = (HttpWebRequest)ar.AsyncState;
    var response = request.EndGetResponse(ar);
}

我有这个例外:

远程服务器返回错误:(405)方法不允许。

当我访问:“ http://example.com/Project/ ”时,页面显示:

Directory Listing Denied

This Virtual Directory does not allow contents to be listed.

但是,我已经chmod 777用于文件夹:project并允许IIS用户上传文件(完全权限)。

为什么我有例外?

我搜索了一个解决方案。 有人建议使用:

NetworkCredential myCred = new NetworkCredential("myusername", "mypassword");
request.Credentials = myCred;

是myusername和mypassword FTP帐户吗?

如果我必须使用FTP帐户,那么我不喜欢这样。 我可以使用其他凭据而不是FTP帐户吗? 因为我不想给ftp帐户,人们会访问我的服务器。

有时,您要“ GetResponse();”的服务器中的IIS配置错误。

为了防止对Web Api的大量请求攻击服务器,IIS检查用户代理,当没有用户代理时,服务器返回405错误。

您必须设置用户代理,例如浏览器用户代理,以便传递此错误。

查看此代码并在您的代码中使用它:

var request = (HttpWebRequest) WebRequest.Create(item.Url);

                request.Method = WebRequestMethods.Http.Get;
                request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; ; rv:1.8.0.7) Gecko/20060917 Firefox/1.9.0.1";
                request.AllowAutoRedirect = true;
                request.Timeout = 1000 * 300;
                request.KeepAlive = false;
                request.ReadWriteTimeout = 1000 * 300;
                request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

转到IIS,然后打开目录浏览并启用它,这应该可以工作

我需要在服务器上添加一个网页来处理上传。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM