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

发布于 2025-01-07 18:12:01 字数 1178 浏览 1 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花开半夏魅人心 2025-01-14 18:12:01

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

go to IIS and then open directory browsing and enable it, this should work

月朦胧 2025-01-14 18:12:01

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

I need to add a webpage on the server to handle the uploading.

暮光沉寂 2025-01-14 18:12:01

有时,您需要“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;

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 :

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文