FileWebRequest Klasse

Definition

Stellt eine Dateisystemimplementierung der WebRequest-Klasse bereit.

public ref class FileWebRequest : System::Net::WebRequest, System::Runtime::Serialization::ISerializable
public ref class FileWebRequest : System::Net::WebRequest
public class FileWebRequest : System.Net.WebRequest, System.Runtime.Serialization.ISerializable
[System.Serializable]
public class FileWebRequest : System.Net.WebRequest, System.Runtime.Serialization.ISerializable
public class FileWebRequest : System.Net.WebRequest
type FileWebRequest = class
    inherit WebRequest
    interface ISerializable
[<System.Serializable>]
type FileWebRequest = class
    inherit WebRequest
    interface ISerializable
Public Class FileWebRequest
Inherits WebRequest
Implements ISerializable
Public Class FileWebRequest
Inherits WebRequest
Vererbung
Attribute
Implementiert

Beispiele

Im folgenden Codebeispiel wird die FileWebRequest -Klasse verwendet, um auf eine Dateisystemressource zuzugreifen.

// This program creates or open a text file in which it stores a string.
// Both file and string are passed by the user.
// Note. In order for this program to work, the folder containing the test file
// must be shared with its permissions set to allow write access.
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::Net;
ref class TestGetRequestStream
{
private:
   static FileWebRequest^ myFileWebRequest;
   static void showUsage()
   {
      Console::WriteLine( "\nPlease enter file name and timeout :" );
      Console::WriteLine( "Usage: cs_getrequeststream <systemname>/<sharedfoldername>/<filename> timeout" );
      Console::WriteLine( "Example: cs_getrequeststream ndpue/temp/hello.txt 1000" );
      Console::WriteLine( "Small timeout values (for instance 3 or less) cause a timeout exception." );
   }

   static void makeFileRequest( String^ fileName, int timeout )
   {
      try
      {
         
         // Create a Uri object.
         Uri^ myUrl = gcnew Uri( String::Format( "file://{0}", fileName ) );
         
         // Create a FileWebRequest object.
         myFileWebRequest = dynamic_cast<FileWebRequest^>(WebRequest::CreateDefault( myUrl ));
         
         // Set the timeout to the value selected by the user.
         myFileWebRequest->Timeout = timeout;
         
         // Set the Method property to POST
         myFileWebRequest->Method = "POST";
         
      }
      catch ( WebException^ e ) 
      {
         Console::WriteLine( "WebException: {0}", e->Message );
      }
      catch ( UriFormatException^ e ) 
      {
         Console::WriteLine( "UriFormatWebException: {0}", e->Message );
      }

   }

   static void writeToFile()
   {
      try
      {
         
         // Enter the string to write into the file.
         Console::WriteLine( "Enter the string you want to write:" );
         String^ userInput = Console::ReadLine();
         
         // Convert the string to Byte array.
         ASCIIEncoding^ encoder = gcnew ASCIIEncoding;
         array<Byte>^byteArray = encoder->GetBytes( userInput );
         
         // Set the ContentLength property.
         myFileWebRequest->ContentLength = byteArray->Length;
         String^ contentLength = myFileWebRequest->ContentLength.ToString();
         Console::WriteLine( "\nThe content length is {0}.", contentLength );
         
         // Get the file stream handler to write into the file.
         Stream^ readStream = myFileWebRequest->GetRequestStream();
         
         // Write to the file stream.
         // Note. In order for this to work the file must be accessible
         // on the network. This can be accomplished by setting the property
         // sharing of the folder containg the file. The permissions
         // can be set so everyone can modify the file.
         // FileWebRequest::Credentials property cannot be used for this purpose.
         readStream->Write( byteArray, 0, userInput->Length );
         Console::WriteLine( "\nThe String you entered was successfully written into the file." );
         
         readStream->Close();
      }
      catch ( WebException^ e ) 
      {
         Console::WriteLine( "The WebException: {0}", e->Message );
      }
      catch ( UriFormatException^ e ) 
      {
         Console::WriteLine( "The UriFormatWebException: {0}", e->Message );
      }

   }


public:
   static void Main()
   {
      array<String^>^args = Environment::GetCommandLineArgs();
      if ( args->Length < 3 )
            showUsage();
      else
      {
         makeFileRequest( args[ 1 ], Int32::Parse( args[ 2 ] ) );
         writeToFile();
      }
   }

};

int main()
{
   TestGetRequestStream::Main();
}
// This example creates or opens a text file and stores a string in it.
// Both the file and the string are passed by the user.
// Note. For this program to work, the folder containing the test file
// must be shared, with its permissions set to allow write access.
using System.Net;
using System;
using System.IO;
using System.Text;

namespace Mssc.PluggableProtocols.File
{
    class TestGetRequestStream
    {
        private static FileWebRequest myFileWebRequest;

        private static void showUsage ()
        {
            Console.WriteLine ("\nPlease enter file name and timeout :");
            Console.WriteLine ("Usage: cs_getrequeststream <systemname>/<sharedfoldername>/<filename> timeout");
            Console.WriteLine ("Example: cs_getrequeststream ngetrequestrtream() ndpue/temp/hello.txt  1000");
            Console.WriteLine ("Small time-out values (for example, 3 or less) cause a time-out exception.");
        }

        private static void makeFileRequest (string fileName, int timeout)
        {
            try
            {
                // Create a Uri object.
                Uri myUrl = new Uri ("file://" + fileName);

                // Create a FileWebRequest object.
                myFileWebRequest = (FileWebRequest)WebRequest.CreateDefault (myUrl);

                // Set the time-out to the value selected by the user.
                myFileWebRequest.Timeout = timeout;

                // Set the Method property to POST
                myFileWebRequest.Method = "POST";
            }
            catch (WebException e)
            {
                Console.WriteLine ("WebException: " + e.Message);
            }
            catch (UriFormatException e)
            {
                Console.WriteLine ("UriFormatWebException: " + e.Message);
            }
        }

        private static void writeToFile ()
        {
            try
            {
                // Enter the string to write to the file.
                Console.WriteLine ("Enter the string you want to write:");

                string userInput = Console.ReadLine ();

                // Convert the string to a byte array.
                ASCIIEncoding encoder = new ASCIIEncoding ();
                byte[] byteArray = encoder.GetBytes (userInput);

                // Set the ContentLength property.
                myFileWebRequest.ContentLength = byteArray.Length;

                string contentLength = myFileWebRequest.ContentLength.ToString ();

                Console.WriteLine ("\nThe content length is {0}.", contentLength);

                // Get the file stream handler to write to the file.
                Stream readStream = myFileWebRequest.GetRequestStream ();

                // Write to the file stream.
                // Note.  For this to work, the file must be accessible
                // on the network. This can be accomplished by setting the property
                // sharing of the folder containg the file.
                // FileWebRequest.Credentials property cannot be used for this purpose.
                readStream.Write (byteArray, 0, userInput.Length);
                Console.WriteLine ("\nThe String you entered was successfully written to the file.");

                readStream.Close ();
            }
            catch (WebException e)
            {
                Console.WriteLine ("The WebException: " + e.Message);
            }
            catch (UriFormatException e)
            {
                Console.WriteLine ("The UriFormatWebException: " + e.Message);
            }
        }

        public static void Main (String[] args)
        {
            if (args.Length < 2)
            {
                showUsage ();
            }
            else
            {
                makeFileRequest (args[0], int.Parse (args[1]));
                writeToFile ();
            }
        }
    }
}
'
' This example creates or opens a text file and stores a string in it. 
' Both the file and the string are passed by the user.
' Note. For this program to work, the folder containing the test file
' must be shared, with its permissions set to allow write access. 

Imports System.Net
Imports System.IO
Imports System.Text

Namespace Mssc.PluggableProtocols.File

    Module TestGetRequestStream

        Class TestGetRequestStream

            Private Shared myFileWebRequest As FileWebRequest

            ' Show how to use this program.
            Private Shared Sub showUsage()
                Console.WriteLine(ControlChars.Lf + "Please enter file name and timeout :")
                Console.WriteLine("Usage: vb_getrequeststream <systemname>/<sharedfoldername>/<filename> timeout")
                Console.WriteLine("Example: vb_getrequeststream ngetrequestrtream() ndpue/temp/hello.txt  1000")
                Console.WriteLine("Small time-out values (for example, 3 or less) cause a time-out exception.")
            End Sub

            Private Shared Sub makeFileRequest(ByVal fileName As String, ByVal timeout As Integer)
                Try
                    ' Create a Uri object.to access the file requested by the user. 
                    Dim myUrl As New Uri("file://" + fileName)

                    ' Create a FileWebRequest object.for the requeste file.
                    myFileWebRequest = CType(WebRequest.CreateDefault(myUrl), FileWebRequest)

                    ' Set the time-out to the value selected by the user.
                    myFileWebRequest.Timeout = timeout

                    ' Set the Method property to POST  
                    myFileWebRequest.Method = "POST"


                Catch e As WebException
                    Console.WriteLine(("WebException is: " + e.Message))
                Catch e As UriFormatException
                    Console.WriteLine(("UriFormatWebException is: " + e.Message))
                End Try

            End Sub

            Private Shared Sub writeToFile()
                Try
                    ' Enter the string to write to the file.
                    Console.WriteLine("Enter the string you want to write:")
                    Dim userInput As String = Console.ReadLine()

                    ' Convert the string to a byte array.
                    Dim encoder As New ASCIIEncoding
                    Dim byteArray As Byte() = encoder.GetBytes(userInput)

                    ' Set the ContentLength property.
                    myFileWebRequest.ContentLength = byteArray.Length

                    Dim contentLength As String = myFileWebRequest.ContentLength.ToString()

                    Console.WriteLine(ControlChars.Lf + "The content length is {0}.", contentLength)


                    ' Get the file stream handler to write to the file.
                    Dim readStream As Stream = myFileWebRequest.GetRequestStream()

                    ' Write to the stream. 
                    ' Note. For this to work the file must be accessible
                    ' on the network. This can be accomplished by setting the property
                    ' sharing of the folder containg the file.  
                    ' FileWebRequest.Credentials property cannot be used for this purpose.
                    readStream.Write(byteArray, 0, userInput.Length)


                    Console.WriteLine(ControlChars.Lf + "The String you entered was successfully written to the file.")

                    readStream.Close()

                Catch e As WebException
                    Console.WriteLine(("WebException is: " + e.Message))
                Catch e As UriFormatException
                    Console.WriteLine(("UriFormatWebException is: " + e.Message))
                End Try

            End Sub

            Public Shared Sub Main(ByVal args() As String)

                If args.Length < 2 Then
                    showUsage()
                Else
                    makeFileRequest(args(0), Integer.Parse(args(1)))
                    writeToFile()
                End If

            End Sub

        End Class



    End Module

End Namespace

Hinweise

Die FileWebRequest -Klasse implementiert die WebRequestabstract Basisklasse für URIs (Uniform Resource Identifiers), die das file:// Schema verwenden, um lokale Dateien anzufordern.

Verwenden Sie nicht die FileWebRequest Konstruktor. Verwenden Sie die WebRequest.Create -Methode, um neue Instanzen der FileWebRequest -Klasse zu initialisieren. Wenn das URI-Schema lautet file://, gibt die Create -Methode ein FileWebRequest -Objekt zurück.

Die GetResponse -Methode stellt eine synchrone Anforderung für die in der RequestUri -Eigenschaft angegebene Datei und gibt ein FileWebResponse -Objekt zurück, das die Antwort enthält. Sie können eine asynchrone Anforderung für die Datei mit den BeginGetResponse Methoden und EndGetResponse ausführen.

Wenn Sie Daten in eine Datei schreiben möchten, gibt die -Methode eine Stream Instanz zurück, in die GetRequestStream geschrieben werden soll. Die BeginGetRequestStream Methoden und EndGetRequestStream bieten asynchronen Zugriff auf den Schreibdatenstrom.

Die FileWebRequest -Klasse basiert auf der -Klasse für die File Fehlerbehandlung und Codezugriffssicherheit.

Konstruktoren

FileWebRequest(SerializationInfo, StreamingContext)
Veraltet.
Veraltet.
Veraltet.

Initialisiert eine neue Instanz der FileWebRequest-Klasse aus den angegebenen Instanzen der SerializationInfo-Klasse und der StreamingContext-Klasse.

Eigenschaften

AuthenticationLevel

Ruft Werte ab, die die für diese Anforderung verwendete Ebene von Authentifizierung und Identitätswechsel angeben, oder legt diese fest.

(Geerbt von WebRequest)
CachePolicy

Ruft die Cacherichtlinie für diese Anforderung ab oder legt diese fest.

(Geerbt von WebRequest)
ConnectionGroupName

Ruft den Namen der Verbindungsgruppe für die Anforderung ab oder legt diesen fest. Diese Eigenschaft ist für eine spätere Verwendung vorgesehen.

ContentLength

Ruft die Länge des Inhalts der gesendeten Daten ab, oder legt diese fest.

ContentType

Ruft den Inhaltstyp der gesendeten Daten ab, oder legt diesen fest. Diese Eigenschaft ist für eine spätere Verwendung vorgesehen.

CreatorInstance
Veraltet.

Ruft beim Überschreiben in einer Nachfolgerklasse das von der IWebRequestCreate-Klasse abgeleitete Factoryobjekt ab. Mit dieser Klasse wird die WebRequest erstellt, die instanziiert wird, um die Anforderung an den angegebenen URI zu stellen.

(Geerbt von WebRequest)
Credentials

Ruft die dieser Anforderung zugeordneten Anmeldeinformationen ab, oder legt diese fest. Diese Eigenschaft ist für eine spätere Verwendung vorgesehen.

Headers

Ruft eine Auflistung der Name/Wert-Paare ab, die der Anforderung zugeordnet sind. Diese Eigenschaft ist für eine spätere Verwendung vorgesehen.

ImpersonationLevel

Ruft die Ebene des Identitätswechsels für die aktuelle Anforderung ab oder legt diese fest.

(Geerbt von WebRequest)
Method

Ruft die für die Anforderung verwendete Protokollmethode ab, oder legt diese fest. Diese Eigenschaft ist für eine spätere Verwendung vorgesehen.

PreAuthenticate

Ruft einen Wert ab, der angibt, ob eine Anforderung vorauthentifiziert werden soll, oder legt diesen fest. Diese Eigenschaft ist für eine spätere Verwendung vorgesehen.

Proxy

Ruft den für diese Anforderung zu verwendenden Netzwerkproxy ab, oder legt diesen fest. Diese Eigenschaft ist für eine spätere Verwendung vorgesehen.

RequestUri

Ruft den URI der Anforderung ab.

Timeout

Ruft die Zeitspanne bis zum Timeout der Anforderung ab, oder legt diese fest.

UseDefaultCredentials

Löst immer eine NotSupportedException aus.

UseDefaultCredentials

Ruft beim Überschreiben in einer Nachfolgerklasse einen Boolean-Wert ab, der steuert, ob mit Anforderungen DefaultCredentials gesendet werden, oder legt einen solchen Wert fest.

(Geerbt von WebRequest)

Methoden

Abort()

Bricht eine Anforderung an eine Internetressource ab.

Abort()

Bricht die Anforderung ab.

(Geerbt von WebRequest)
BeginGetRequestStream(AsyncCallback, Object)

Startet eine asynchrone Anforderung eines Stream-Objekts, das zum Schreiben von Daten verwendet werden soll.

BeginGetResponse(AsyncCallback, Object)

Startet eine asynchrone Anforderung einer Dateisystemressource.

CreateObjRef(Type)

Erstellt ein Objekt mit allen relevanten Informationen, die zum Generieren eines Proxys für die Kommunikation mit einem Remoteobjekt erforderlich sind.

(Geerbt von MarshalByRefObject)
EndGetRequestStream(IAsyncResult)

Beendet eine asynchrone Anforderung einer Stream-Instanz, die von der Anwendung zum Schreiben von Daten verwendet wird.

EndGetResponse(IAsyncResult)

Beendet eine asynchrone Anforderung einer Dateisystemressource.

Equals(Object)

Bestimmt, ob das angegebene Objekt gleich dem aktuellen Objekt ist.

(Geerbt von Object)
GetHashCode()

Fungiert als Standardhashfunktion.

(Geerbt von Object)
GetLifetimeService()
Veraltet.

Ruft das aktuelle Lebensdauerdienstobjekt ab, das die Lebensdauerrichtlinien für diese Instanz steuert.

(Geerbt von MarshalByRefObject)
GetObjectData(SerializationInfo, StreamingContext)
Veraltet.

Füllt eine SerializationInfo mit den Daten auf, die zum Serialisieren des Zielobjekts erforderlich sind.

GetObjectData(SerializationInfo, StreamingContext)
Veraltet.

Füllt eine SerializationInfo mit den Daten auf, die zum Serialisieren des Zielobjekts erforderlich sind.

(Geerbt von WebRequest)
GetRequestStream()

Gibt ein Stream-Objekt zum Schreiben von Daten in die Dateisystemressource zurück.

GetRequestStreamAsync()

Gibt einen Stream für das Schreiben von Daten in die Dateisystemressource als asynchroner Vorgang zurück

GetRequestStreamAsync()

Gibt nach dem Überschreiben in einer abgeleiteten Klasse einen Stream zurück, womit Daten in einem asynchronen Vorgang in die Internetressource geschrieben werden können.

(Geerbt von WebRequest)
GetResponse()

Gibt eine Antwort auf eine Dateisystemanforderung zurück.

GetResponseAsync()

Gibt eine Antwort auf eine Dateisystemanforderung als asynchronen Vorgang zurück

GetResponseAsync()

Gibt beim Überschreiben in einer Nachfolgerklasse in einem asynchronen Vorgang eine Antwort auf eine Internetanforderung zurück.

(Geerbt von WebRequest)
GetType()

Ruft den Type der aktuellen Instanz ab.

(Geerbt von Object)
InitializeLifetimeService()
Veraltet.

Ruft ein Lebensdauerdienstobjekt zur Steuerung der Lebensdauerrichtlinie für diese Instanz ab.

(Geerbt von MarshalByRefObject)
MemberwiseClone()

Erstellt eine flache Kopie des aktuellen Object.

(Geerbt von Object)
MemberwiseClone(Boolean)

Erstellt eine flache Kopie des aktuellen MarshalByRefObject-Objekts.

(Geerbt von MarshalByRefObject)
ToString()

Gibt eine Zeichenfolge zurück, die das aktuelle Objekt darstellt.

(Geerbt von Object)

Explizite Schnittstellenimplementierungen

ISerializable.GetObjectData(SerializationInfo, StreamingContext)
Veraltet.

Füllt ein SerializationInfo-Objekt mit den zum Serialisieren der FileWebRequest erforderlichen Daten auf.

Gilt für:

Weitere Informationen