Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.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# WebRequest POST返回“;远程服务器返回错误:(405)方法不允许;_C#_.net - Fatal编程技术网

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

C# WebRequest POST返回“;远程服务器返回错误:(405)方法不允许;,c#,.net,C#,.net,我正在使用WebRequest通过控制台应用程序将文件发布到组织中的服务器。在网上做了一些研究之后,我终于想出了下面的代码 try { RegisterString("Uploading encrypted file to server....Please wait!!"); string url = @"http://localhost:3333/MySite/" string filepath = @"C:\test.txt"; WebRequest requ

我正在使用WebRequest通过控制台应用程序将文件发布到组织中的服务器。在网上做了一些研究之后,我终于想出了下面的代码

try
{
    RegisterString("Uploading encrypted file to server....Please wait!!");
    string url = @"http://localhost:3333/MySite/"
    string filepath = @"C:\test.txt";
    WebRequest request = WebRequest.Create(url);
    request.Method = "POST";
    request.PreAuthenticate = true;
    request.Credentials = new NetworkCredential(ftp_username, ftp_password);
    byte[] byteArray = Encoding.UTF8.GetBytes(filePath);
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = byteArray.Length;


    //Here is the Business end of the code...
    Stream dataStream = request.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();
    //and here is the response.
    WebResponse response = request.GetResponse();

    Console.WriteLine(((HttpWebResponse)response).StatusDescription);
    dataStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(dataStream);
    string responseFromServer = reader.ReadToEnd();
    Console.WriteLine(responseFromServer);
    reader.Close();
    dataStream.Close();
    response.Close();

    RegisterString("File uploaded sucessfully");                

    try
    {
        //Delete file after transmission
        File.Delete(filePath);
    }
    catch (Exception ex)
    {
        RegisterString(ex.Message);
    }

    WriteToLog("End");
    System.Threading.Thread.Sleep(5000);
}
catch (Exception ex)
{
    RegisterString(ex.Message);
    System.Threading.Thread.Sleep(5000);
    WriteToLog("End");
}

但是我得到了异常“405方法不允许”。有什么我遗漏的吗?

是ftp服务器吗?如果是这样的话,该方法应该被放置,而不是发布。

因为响应明确地抱怨该方法-您是否已经尝试了
request.method=“GET”
(而不是“POST”)?虽然我认为这与操作另一端的代码有很大关系,但带有二进制数据的HTTP POST的mime类型通常是
多部分/表单数据
。您发布到的服务可能不需要标准的表单帖子,但我仍然希望您的内容类型应该是数据的实际格式,而不是
application/x-www-form-urlencoded
。一旦我将contenttype更改为multipart/form data。它抛出另一个错误(404)文件未找到)它是一个Web服务器…我正在使用put。下面是一个示例:WebClient=newWebClient();CredentialCache credCache=新的CredentialCache();credCache.Add(新Uri(@ftp_url+filePath),“Basic”,新网络凭证(ftp_用户名,ftp_密码,“非洲”);client.Credentials=credCache;Uri地址=新的Uri(@ftp\uURL+filePath);WriteToLog(@ftp\uurl+filePath);字节[]arrrurn=client.UploadFile(地址“PUT”,文件路径);