简体   繁体   English

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

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

I want to use HttpWebRequest to post a file to the server: 我想使用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);
}

I got this exception: 我有这个例外:

The remote server returned an error: (405) Method Not Allowed. 远程服务器返回错误:(405)方法不允许。

When I access: " http://example.com/Project/ ", the page shows: 当我访问:“ http://example.com/Project/ ”时,页面显示:

Directory Listing Denied

This Virtual Directory does not allow contents to be listed.

However, I already chmod 777 for the folder: project and allow IIS user to upload files on it (full permission). 但是,我已经chmod 777用于文件夹:project并允许IIS用户上传文件(完全权限)。

Why I got that exception? 为什么我有例外?

I searched for a solution. 我搜索了一个解决方案。 Some people advices to use: 有人建议使用:

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

Is myusername and mypassword the account of the FTP? 是myusername和mypassword FTP帐户吗?

If I have to use FTP account, then I don't like that. 如果我必须使用FTP帐户,那么我不喜欢这样。 Can I use some other credentials rather then FTP account? 我可以使用其他凭据而不是FTP帐户吗? Because I don't want to give the ftp account and people will access on my server. 因为我不想给ftp帐户,人们会访问我的服务器。

Sometimes this error for IIS config in the server you want "GetResponse();". 有时,您要“ GetResponse();”的服务器中的IIS配置错误。

For preventing attack the server by lots of request to Web Api , IIS check user-agent and when there is not user-agent Server return 405 error. 为了防止对Web Api的大量请求攻击服务器,IIS检查用户代理,当没有用户代理时,服务器返回405错误。

You must set user-agent for example browser user-agent for passing this error. 您必须设置用户代理,例如浏览器用户代理,以便传递此错误。

see this code and use it in your code : 查看此代码并在您的代码中使用它:

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.

相关问题 远程服务器返回错误:(405)允许方法 - The remote server returned an error: (405) Method Allowed 远程服务器返回错误:“(405) Method Not Allowed” - The remote server returned an error: "(405) Method Not Allowed" 远程服务器返回错误:(405)mvc 5中不允许使用方法 - The remote server returned an error: (405) Method Not Allowed in mvc 5 Azure移动服务,远程服务器返回错误:(405)不允许的方法 - Azure mobile service, The remote server returned an error: (405) Method Not Allowed .NET,远程服务器返回错误:(405) Method Not Allowed - .NET , The remote server returned an error: (405) Method Not Allowed 远程服务器返回错误:(405) 在 expedia api 调用中不允许方法 - The remote server returned an error: (405) Method Not Allowed in expedia api call 调用web api发布方法错误“远程服务器返回错误:(405)不允许使用方法” - calling web api Post method error “The remote server returned an error: (405) Method Not Allowed” 远程服务器返回错误:(405) Method Not Allowed service request and "Request method 'POST' not supported" - The remote server returned an error: (405) Method Not Allowed service request and "Request method 'POST' not supported" 交换Web服务错误-远程服务器返回不允许的错误405方法 - exchange web service error - the remote server returned an error 405 method not allowed WCF-远程服务器返回了意外的响应:(405)不允许的方法 - WCF - The remote server returned an unexpected response: (405) Method Not Allowed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM