0

我正在使用 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 Method Not Allowed”。有什么我想念的吗?

4

1 回答 1

0

是ftp服务器吗?如果是这样,方法应该是 PUT,而不是 POST。

于 2012-05-05T15:25:26.343 回答