FileWebRequest クラス

定義

WebRequest クラスのファイル システム実装を提供します。

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
継承
属性
実装

次のコード例では、FileWebRequest クラスを使用してファイル システム リソースにアクセスします。

// 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

注釈

FileWebRequest クラスは、file:// スキームを使用してローカル ファイルを要求する URI (Uniform Resource Identifier) の WebRequestabstract 基本クラスを実装します。

FileWebRequest コンストラクターは使用しないでください。 WebRequest.Create メソッドを使用して、FileWebRequest クラスの新しいインスタンスを初期化します。 URI スキームが file://されている場合、Create メソッドは FileWebRequest オブジェクトを返します。

GetResponse メソッドは、RequestUri プロパティで指定されたファイルに対して同期要求を行い、応答を含む FileWebResponse オブジェクトを返します。 BeginGetResponse メソッドと EndGetResponse メソッドを使用して、ファイルの非同期要求を行うことができます。

ファイルにデータを書き込む場合、GetRequestStream メソッドは書き込み対象の Stream インスタンスを返します。 BeginGetRequestStream メソッドと EndGetRequestStream メソッドは、書き込みデータ ストリームへの非同期アクセスを提供します。

FileWebRequest クラスは、エラー処理とコード アクセスセキュリティのために File クラスに依存します。

コンストラクター

FileWebRequest(SerializationInfo, StreamingContext)
古い.
古い.
古い.

SerializationInfo クラスと StreamingContext クラスの指定したインスタンスから、FileWebRequest クラスの新しいインスタンスを初期化します。

プロパティ

AuthenticationLevel

この要求に使用される認証と偽装のレベルを示す値を取得または設定します。

(継承元 WebRequest)
CachePolicy

この要求のキャッシュ ポリシーを取得または設定します。

(継承元 WebRequest)
ConnectionGroupName

要求の接続グループの名前を取得または設定します。 このプロパティは、将来使用するために予約されています。

ContentLength

送信されるデータのコンテンツの長さを取得または設定します。

ContentType

送信されるデータのコンテンツ タイプを取得または設定します。 このプロパティは、将来使用するために予約されています。

CreatorInstance
古い.

子孫クラスでオーバーライドされると、指定した URI への要求を行うためにインスタンス化された WebRequest を作成するために使用される IWebRequestCreate クラスから派生したファクトリ オブジェクトを取得します。

(継承元 WebRequest)
Credentials

この要求に関連付けられている資格情報を取得または設定します。 このプロパティは、将来使用するために予約されています。

Headers

要求に関連付けられている名前と値のペアのコレクションを取得します。 このプロパティは、将来使用するために予約されています。

ImpersonationLevel

現在の要求の偽装レベルを取得または設定します。

(継承元 WebRequest)
Method

要求に使用されるプロトコル メソッドを取得または設定します。 このプロパティは、将来使用するために予約されています。

PreAuthenticate

要求を事前認証するかどうかを示す値を取得または設定します。 このプロパティは、将来使用するために予約されています。

Proxy

この要求に使用するネットワーク プロキシを取得または設定します。 このプロパティは、将来使用するために予約されています。

RequestUri

要求の URI (Uniform Resource Identifier) を取得します。

Timeout

要求がタイムアウトするまでの時間を取得または設定します。

UseDefaultCredentials

常に NotSupportedExceptionをスローします。

UseDefaultCredentials

子孫クラスでオーバーライドされると、DefaultCredentials が要求と共に送信されるかどうかを制御する Boolean 値を取得または設定します。

(継承元 WebRequest)

メソッド

Abort()

インターネット リソースへの要求を取り消します。

Abort()

要求を中止します。

(継承元 WebRequest)
BeginGetRequestStream(AsyncCallback, Object)

データの書き込みに使用する Stream オブジェクトの非同期要求を開始します。

BeginGetResponse(AsyncCallback, Object)

ファイル システム リソースの非同期要求を開始します。

CreateObjRef(Type)

リモート オブジェクトとの通信に使用されるプロキシの生成に必要なすべての関連情報を含むオブジェクトを作成します。

(継承元 MarshalByRefObject)
EndGetRequestStream(IAsyncResult)

アプリケーションがデータの書き込みに使用する Stream インスタンスの非同期要求を終了します。

EndGetResponse(IAsyncResult)

ファイル システム リソースの非同期要求を終了します。

Equals(Object)

指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetLifetimeService()
古い.

このインスタンスの有効期間ポリシーを制御する現在の有効期間サービス オブジェクトを取得します。

(継承元 MarshalByRefObject)
GetObjectData(SerializationInfo, StreamingContext)
古い.

ターゲット オブジェクトをシリアル化するために必要なデータを SerializationInfo に設定します。

GetObjectData(SerializationInfo, StreamingContext)
古い.

ターゲット オブジェクトをシリアル化するために必要なデータを SerializationInfo に設定します。

(継承元 WebRequest)
GetRequestStream()

ファイル システム リソースにデータを書き込むための Stream オブジェクトを返します。

GetRequestStreamAsync()

非同期操作としてファイル システム リソースにデータを書き込むためのストリームを返します。

GetRequestStreamAsync()

子孫クラスでオーバーライドされると、非同期操作としてインターネット リソースにデータを書き込むための Stream を返します。

(継承元 WebRequest)
GetResponse()

ファイル システム要求への応答を返します。

GetResponseAsync()

非同期操作としてファイル システム要求への応答を返します。

GetResponseAsync()

子孫クラスでオーバーライドされると、インターネット要求への応答を非同期操作として返します。

(継承元 WebRequest)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
InitializeLifetimeService()
古い.

このインスタンスの有効期間ポリシーを制御する有効期間サービス オブジェクトを取得します。

(継承元 MarshalByRefObject)
MemberwiseClone()

現在の Objectの簡易コピーを作成します。

(継承元 Object)
MemberwiseClone(Boolean)

現在の MarshalByRefObject オブジェクトの簡易コピーを作成します。

(継承元 MarshalByRefObject)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

ISerializable.GetObjectData(SerializationInfo, StreamingContext)
古い.

FileWebRequestをシリアル化するために必要なデータを SerializationInfo オブジェクトに設定します。

適用対象

こちらもご覧ください