远程服务器返回错误:(405) 不允许方法
我想使用 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.
但是,我已经对文件夹:project 进行了 chmod 777 并允许 IIS 用户在其上上传文件(完全权限)。
为什么我会得到这个例外?
我寻找解决方案。有人建议使用:
NetworkCredential myCred = new NetworkCredential("myusername", "mypassword");
request.Credentials = myCred;
myusername和mypassword是FTP的帐户吗?
如果我必须使用 FTP 帐户,那么我不喜欢那样。我可以使用其他凭据而不是 FTP 帐户吗?因为我不想提供 ftp 帐户,人们会在我的服务器上访问。
I want to use HttpWebRequest to post a file to the server:
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.
When I access: "http://example.com/Project/", the page shows:
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).
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?
If I have to use FTP account, then I don't like that. Can I use some other credentials rather then FTP account? Because I don't want to give the ftp account and people will access on my server.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
转到 IIS,然后打开目录浏览并启用它,这应该可以工作
go to IIS and then open directory browsing and enable it, this should work
我需要在服务器上添加一个网页来处理上传。
I need to add a webpage on the server to handle the uploading.
有时,您需要“GetResponse();”的服务器中的 IIS 配置会出现此错误。
为了防止大量对 Web Api 的请求攻击服务器,IIS 检查用户代理,当没有用户代理时服务器返回 405 错误。
您必须设置用户代理(例如浏览器用户代理)才能传递此错误。
查看此代码并在您的代码中使用它:
Sometimes this error for IIS config in the server you want "GetResponse();".
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.
You must set user-agent for example browser user-agent for passing this error.
see this code and use it in your code :