Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 远程服务器返回错误:(405)方法不允许_C#_Httpwebrequest_Webrequest - Fatal编程技术网

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

C# 远程服务器返回错误:(405)方法不允许,c#,httpwebrequest,webrequest,C#,Httpwebrequest,Webrequest,我想使用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";

我想使用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)方法不允许

当我访问“”时,页面显示:

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;
我的用户名和密码是FTP的帐户吗


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

转到IIS,然后打开目录浏览并启用它,这应该会起作用。

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

有时,服务器中的IIS配置会出现此错误,您需要“GetResponse();”

为了防止对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;

您需要为虚拟目录设置默认文档。默认文档是什么?如何设置它?用户名是域名,例如,如果您的域是mydomain.com,您的用户应该是mydomain.com\username,这有意义吗,或者这是通过FtpWebRequest ftp=(FtpWebRequest)FtpWebRequest.Create(ftpfullpath)。。?那么您的用户名将是FTP帐户用户名和FTPpassword@BrianDriscoll我添加了index.html,我可以查看链接,但我得到了相同的结果exception@DJKRAZE对不起,我不明白。我在哪里可以从我的主机上获取该用户:mydomain.com\username和password?这是不确定的,因为用户将浏览文件夹并可以下载所有内容