[英]WebRequest POST returns “The remote server returned an error: (405) Method Not Allowed.”
I'm using WebRequest to POST a file through a console application to an server in my organisation. 我正在使用WebRequest通过控制台应用程序将文件发布到组织中的服务器。 After some research on-line, I was able to come up with the code below;
经过一些在线研究,我得以提出以下代码;
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");
}
However I get the exception "405 Method Not Allowed". 但是,我得到了异常“ 405方法不允许”。 Is there something that I'm missing?
有什么我想念的吗?
Is it an ftp server? 它是ftp服务器吗? If so the method should be PUT, not POST.
如果是这样,则该方法应为PUT,而不是POST。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.