FtpWebRequest.GetResponse 方法

定义

返回 FTP 服务器响应。

public:
 override System::Net::WebResponse ^ GetResponse();
public override System.Net.WebResponse GetResponse ();
override this.GetResponse : unit -> System.Net.WebResponse
Public Overrides Function GetResponse () As WebResponse

返回

包含一个 FtpWebResponse 实例的 WebResponse 引用。 此对象包含 FTP 服务器对请求的响应。

例外

已经为此实例调用了 GetResponse()BeginGetResponse(AsyncCallback, Object)

- 或 -

HTTP 代理被启用,而您尝试使用 FTP 命令而非 DownloadFileListDirectoryListDirectoryDetails

EnableSsl 设置为 true,但服务器不支持此功能。

- 或 -

Timeout 已指定,且已超时。

示例

下面的代码示例演示如何将文件复制到请求的数据流,并发送请求以将数据追加到文件到服务器。 该示例调用 GetResponse 发送请求并阻止,直到服务器返回响应。

static bool AppendFileOnServer( String^ fileName, Uri^ serverUri )
{
   // The URI described by serverUri should use the ftp:// scheme.
   // It contains the name of the file on the server.
   // Example: ftp://contoso.com/someFile.txt. 
   // The fileName parameter identifies the file containing 
   // the data to be appended to the file on the server.
   if ( serverUri->Scheme != Uri::UriSchemeFtp )
   {
      return false;
   }

   // Get the object used to communicate with the server.
   FtpWebRequest^ request = dynamic_cast<FtpWebRequest^>(WebRequest::Create( serverUri ));
   request->Method = WebRequestMethods::Ftp::AppendFile;
   StreamReader^ sourceStream = gcnew StreamReader( fileName );
   array<Byte>^fileContents = Encoding::UTF8->GetBytes( sourceStream->ReadToEnd() );
   sourceStream->Close();
   request->ContentLength = fileContents->Length;

   // This example assumes the FTP site uses anonymous logon.
   request->Credentials = gcnew NetworkCredential( "anonymous","janeDoe@contoso.com" );
   Stream^ requestStream = request->GetRequestStream();
   requestStream->Write( fileContents, 0, fileContents->Length );
   requestStream->Close();
   FtpWebResponse^ response = dynamic_cast<FtpWebResponse^>(request->GetResponse());
   Console::WriteLine( "Append status: {0}", response->StatusDescription );
   response->Close();
   return true;
}
public static bool AppendFileOnServer(string fileName, Uri serverUri)
{
    // The URI described by serverUri should use the ftp:// scheme.
    // It contains the name of the file on the server.
    // Example: ftp://contoso.com/someFile.txt.
    // The fileName parameter identifies the file containing
    // the data to be appended to the file on the server.

    if (serverUri.Scheme != Uri.UriSchemeFtp)
    {
        return false;
    }
    // Get the object used to communicate with the server.
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
    request.Method = WebRequestMethods.Ftp.AppendFile;

    StreamReader sourceStream = new StreamReader(fileName);
    byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
    sourceStream.Close();
    request.ContentLength = fileContents.Length;

    // This example assumes the FTP site uses anonymous logon.
    request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
    Stream requestStream = request.GetRequestStream();
    requestStream.Write(fileContents, 0, fileContents.Length);
    requestStream.Close();
    FtpWebResponse response = (FtpWebResponse) request.GetResponse();

    Console.WriteLine("Append status: {0}",response.StatusDescription);

    response.Close();
    return true;
}

注解

若要访问特定于 FTP 的属性,必须将此方法返回的对象强制转换为 WebResponseFtpWebResponse

GetResponse 导致建立控制连接,还可能会创建数据连接。 GetResponse 阻止,直到收到响应。 若要防止这种情况,可以通过调用 BeginGetResponseEndGetResponse 方法代替 GetResponse来异步执行此操作。

Proxy如果直接或在配置文件中设置了 属性,则通过代理与 FTP 服务器的通信。

WebException如果引发 ,请使用 Response 异常的 和 Status 属性来确定来自服务器的响应。

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅.NET Framework中的网络跟踪

注意

多次调用以 GetResponse 返回相同的响应对象;请求不会重新发出。

调用方说明

此方法生成网络流量。

适用于

另请参阅