TechNet
Export (0) Print
Expand All

FileWebRequest Class

 

Provides a file system implementation of the WebRequest class.

Namespace:   System.Net
Assembly:  System (in System.dll)

System.Object
  System.MarshalByRefObject
    System.Net.WebRequest
      System.Net.FileWebRequest

[SerializableAttribute]
public class FileWebRequest : WebRequest, ISerializable

NameDescription
System_CAPS_protmethodFileWebRequest(SerializationInfo, StreamingContext)

Obsolete.Initializes a new instance of the FileWebRequest class from the specified instances of the SerializationInfo and StreamingContext classes.

NameDescription
System_CAPS_pubpropertyAuthenticationLevel

Gets or sets values indicating the level of authentication and impersonation used for this request.(Inherited from WebRequest.)

System_CAPS_pubpropertyCachePolicy

Gets or sets the cache policy for this request.(Inherited from WebRequest.)

System_CAPS_pubpropertyConnectionGroupName

Gets or sets the name of the connection group for the request. This property is reserved for future use.(Overrides WebRequest.ConnectionGroupName.)

System_CAPS_pubpropertyContentLength

Gets or sets the content length of the data being sent.(Overrides WebRequest.ContentLength.)

System_CAPS_pubpropertyContentType

Gets or sets the content type of the data being sent. This property is reserved for future use.(Overrides WebRequest.ContentType.)

System_CAPS_pubpropertyCreatorInstance

Obsolete.When overridden in a descendant class, gets the factory object derived from the IWebRequestCreate class used to create the WebRequest instantiated for making the request to the specified URI.(Inherited from WebRequest.)

System_CAPS_pubpropertyCredentials

Gets or sets the credentials that are associated with this request. This property is reserved for future use.(Overrides WebRequest.Credentials.)

System_CAPS_pubpropertyHeaders

Gets a collection of the name/value pairs that are associated with the request. This property is reserved for future use.(Overrides WebRequest.Headers.)

System_CAPS_pubpropertyImpersonationLevel

Gets or sets the impersonation level for the current request.(Inherited from WebRequest.)

System_CAPS_pubpropertyMethod

Gets or sets the protocol method used for the request. This property is reserved for future use.(Overrides WebRequest.Method.)

System_CAPS_pubpropertyPreAuthenticate

Gets or sets a value that indicates whether to preauthenticate a request. This property is reserved for future use.(Overrides WebRequest.PreAuthenticate.)

System_CAPS_pubpropertyProxy

Gets or sets the network proxy to use for this request. This property is reserved for future use.(Overrides WebRequest.Proxy.)

System_CAPS_pubpropertyRequestUri

Gets the Uniform Resource Identifier (URI) of the request.(Overrides WebRequest.RequestUri.)

System_CAPS_pubpropertyTimeout

Gets or sets the length of time until the request times out.(Overrides WebRequest.Timeout.)

System_CAPS_pubpropertyUseDefaultCredentials

This API supports the product infrastructure and is not intended to be used directly from your code. Always throws a NotSupportedException.(Overrides WebRequest.UseDefaultCredentials.)

NameDescription
System_CAPS_pubmethodAbort()

Cancels a request to an Internet resource.(Overrides WebRequest.Abort().)

System_CAPS_pubmethodBeginGetRequestStream(AsyncCallback, Object)

Begins an asynchronous request for a Stream object to use to write data.(Overrides WebRequest.BeginGetRequestStream(AsyncCallback, Object).)

System_CAPS_pubmethodBeginGetResponse(AsyncCallback, Object)

Begins an asynchronous request for a file system resource.(Overrides WebRequest.BeginGetResponse(AsyncCallback, Object).)

System_CAPS_pubmethodCreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodEndGetRequestStream(IAsyncResult)

Ends an asynchronous request for a Stream instance that the application uses to write data.(Overrides WebRequest.EndGetRequestStream(IAsyncResult).)

System_CAPS_pubmethodEndGetResponse(IAsyncResult)

Ends an asynchronous request for a file system resource.(Overrides WebRequest.EndGetResponse(IAsyncResult).)

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetLifetimeService()

Retrieves the current lifetime service object that controls the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_protmethodGetObjectData(SerializationInfo, StreamingContext)

Populates a SerializationInfo with the data needed to serialize the target object.(Overrides WebRequest.GetObjectData(SerializationInfo, StreamingContext).)

System_CAPS_pubmethodGetRequestStream()

Returns a Stream object for writing data to the file system resource.(Overrides WebRequest.GetRequestStream().)

System_CAPS_pubmethodGetRequestStreamAsync()

When overridden in a descendant class, returns a Stream for writing data to the Internet resource as an asynchronous operation.(Inherited from WebRequest.)

System_CAPS_pubmethodGetResponse()

Returns a response to a file system request.(Overrides WebRequest.GetResponse().)

System_CAPS_pubmethodGetResponseAsync()

When overridden in a descendant class, returns a response to an Internet request as an asynchronous operation.(Inherited from WebRequest.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodInitializeLifetimeService()

Obtains a lifetime service object to control the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_privmethodISerializable.GetObjectData(SerializationInfo, StreamingContext)

This API supports the product infrastructure and is not intended to be used directly from your code. Populates a SerializationInfo object with the required data to serialize the FileWebRequest.

The FileWebRequest class implements the WebRequestabstract base class for Uniform Resource Identifiers (URIs) that use the file:// scheme to request local files.

Do not use the FileWebRequest constructor. Use the WebRequest.Create method to initialize new instances of the FileWebRequest class. If the URI scheme is file://, the Create method returns a FileWebRequest object.

The GetResponse method makes a synchronous request for the file specified in the RequestUri property and returns a FileWebResponse object that contains the response. You can make an asynchronous request for the file using the BeginGetResponse and EndGetResponse methods.

When you want to write data to a file, the GetRequestStream method returns a Stream instance to write to. The BeginGetRequestStream and EndGetRequestStream methods provide asynchronous access to the write data stream.

The FileWebRequest class relies on the File class for error handling and code access security.

The following code example uses the FileWebRequest class to access a file system resource.

// 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 ();
            }
        }
    }
}

.NET Framework
Available since 1.1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show:
© 2016 Microsoft