Export (0) Print
Expand All
0 out of 3 rated this helpful - Rate this topic

FileWebRequest Class

Provides a file system implementation of the WebRequest class.

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

[SerializableAttribute] 
public class FileWebRequest : WebRequest, ISerializable
/** @attribute SerializableAttribute() */ 
public class FileWebRequest extends WebRequest implements ISerializable
SerializableAttribute 
public class FileWebRequest extends WebRequest implements ISerializable

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

// 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. 
import System.Net.*;
import System.*;
import System.IO.*;
import System.Text.*;

class TestGetRequestStream
{
    private static FileWebRequest myFileWebRequest;

    private static void ShowUsage()
    {
        Console.WriteLine("\nPlease enter file name and timeout :");
        Console.WriteLine("Usage: jsl_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.");
    } //ShowUsage

    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.set_Timeout(timeout);
            // Set the Method property to POST  
            myFileWebRequest.set_Method("POST");
        }
        catch (WebException e) {
            Console.WriteLine(("WebException: " + e.get_Message()));
        }
        catch (UriFormatException e) {
            Console.WriteLine(("UriFormatWebException: " + e.get_Message()));
        }
    } //MakeFileRequest

    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();
            ubyte byteArray[] = encoder.GetBytes(userInput);

            // Set the ContentLength property.
            myFileWebRequest.set_ContentLength(byteArray.length);
            String contentLength = 
                (new Long(myFileWebRequest.get_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.get_Length());
            Console.WriteLine("\nThe String you entered was successfully " 
                + "written to the file.");
            readStream.Close();
        }
        catch (WebException e) {
            Console.WriteLine(("The WebException: " + e.get_Message()));
        }
        catch (UriFormatException e) {
            Console.WriteLine(("The UriFormatWebException: " 
                + e.get_Message()));
        }
    } //WriteToFile

    public static void main(String[] args)
    {
        if (args.length < 2) {
            ShowUsage();
        }
        else {
            MakeFileRequest(args[0], Integer.parseInt(args[1]));
            WriteToFile();
        }
    } //main
} //TestGetRequestStream

System.Object
   System.MarshalByRefObject
     System.Net.WebRequest
      System.Net.FileWebRequest
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.