工作者对象HttpWorkerRequest
在ASP.NET中,用于处理的请求,需要封装为HttpWorkerRequest类型的对象。该类为抽象类,定义在命名空间System.Web下。
#region Assembly System.Web.dll, v4.0.0.0 // C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Web.dll #endregion using System; using System.Runtime.InteropServices; using System.Security.Principal; using System.Text; using System.Web.Management; namespace System.Web { // Summary: // This abstract class defines the base worker methods and enumerations used // by ASP.NET managed code to process requests. [ComVisible(false)] public abstract class HttpWorkerRequest { // Summary: // Specifies the index number for the Accept HTTP header. public const int HeaderAccept = 20; // // Summary: // Specifies the index number for the Accept-Charset HTTP header. public const int HeaderAcceptCharset = 21; // // Summary: // Specifies the index number for the Accept-Encoding HTTP header. public const int HeaderAcceptEncoding = 22; // // Summary: // Specifies the index number for the Accept-Language HTTP header. public const int HeaderAcceptLanguage = 23; // // Summary: // Specifies the index number for the Accept-Ranges HTTP header. public const int HeaderAcceptRanges = 20; // // Summary: // Specifies the index number for the Age HTTP header. public const int HeaderAge = 21; // // Summary: // Specifies the index number for the Allow HTTP header. public const int HeaderAllow = 10; // // Summary: // Specifies the index number for the Authorization HTTP header. public const int HeaderAuthorization = 24; // // Summary: // The index that represents the HTTP Cache-Control HTTP header. public const int HeaderCacheControl = 0; // // Summary: // Specifies the index number for the Connection HTTP header. public const int HeaderConnection = 1; // // Summary: // Specifies the index number for the Content-Encoding HTTP header. public const int HeaderContentEncoding = 13; // // Summary: // Specifies the index number for the Content-Language HTTP header. public const int HeaderContentLanguage = 14; // // Summary: // Specifies the index number for the Content-Length HTTP header. public const int HeaderContentLength = 11; // // Summary: // Specifies the index number for the Content-Location HTTP header. public const int HeaderContentLocation = 15; // // Summary: // Specifies the index number for the Content-MD5 HTTP header. public const int HeaderContentMd5 = 16; // // Summary: // Specifies the index number for the Content-Range HTTP header. public const int HeaderContentRange = 17; // // Summary: // Specifies the index number for the Content-Type HTTP header. public const int HeaderContentType = 12; // // Summary: // Specifies the index number for the Cookie HTTP header. public const int HeaderCookie = 25; // // Summary: // Specifies the index number for the Date HTTP header. public const int HeaderDate = 2; // // Summary: // Specifies the index number for the ETag HTTP header. public const int HeaderEtag = 22; // // Summary: // Specifies the index number for the Except HTTP header. public const int HeaderExpect = 26; // // Summary: // Specifies the index number for the Expires HTTP header. public const int HeaderExpires = 18; // // Summary: // Specifies the index number for the From HTTP header. public const int HeaderFrom = 27; // // Summary: // Specifies the index number for the Host HTTP header. public const int HeaderHost = 28; // // Summary: // Specifies the index number for the If-Match HTTP header. public const int HeaderIfMatch = 29; // // Summary: // Specifies the index number for the If-Modified-Since HTTP header. public const int HeaderIfModifiedSince = 30; // // Summary: // Specifies the index number for the If-None-Match HTTP header. public const int HeaderIfNoneMatch = 31; // // Summary: // Specifies the index number for the If-Range HTTP header. public const int HeaderIfRange = 32; // // Summary: // Specifies the index number for the If-Unmodified-Since HTTP header. public const int HeaderIfUnmodifiedSince = 33; // // Summary: // Specifies the index number for the Keep-Alive HTTP header. public const int HeaderKeepAlive = 3; // // Summary: // Specifies the index number for the Last-Modified HTTP header. public const int HeaderLastModified = 19; // // Summary: // Specifies the index number for the Location HTTP header. public const int HeaderLocation = 23; // // Summary: // Specifies the index number for the Max-Forwards HTTP header. public const int HeaderMaxForwards = 34; // // Summary: // Specifies the index number for the Pragma HTTP header. public const int HeaderPragma = 4; // // Summary: // Specifies the index number for the Proxy-Authenticate HTTP header. public const int HeaderProxyAuthenticate = 24; // // Summary: // Specifies the index number for the Proxy-Authorization HTTP header. public const int HeaderProxyAuthorization = 35; // // Summary: // Specifies the index number for the Range HTTP header. public const int HeaderRange = 37; // // Summary: // Specifies the index number for the Referer HTTP header. public const int HeaderReferer = 36; // // Summary: // Specifies the index number for the Retry-After HTTP header. public const int HeaderRetryAfter = 25; // // Summary: // Specifies the index number for the Server HTTP header. public const int HeaderServer = 26; // // Summary: // Specifies the index number for the Set-Cookie HTTP header. public const int HeaderSetCookie = 27; // // Summary: // Specifies the index number for the TE HTTP header. public const int HeaderTe = 38; // // Summary: // Specifies the index number for the Trailer HTTP header. public const int HeaderTrailer = 5; // // Summary: // Specifies the index number for the Transfer-Encoding HTTP header. public const int HeaderTransferEncoding = 6; // // Summary: // Specifies the index number for the Upgrade HTTP header. public const int HeaderUpgrade = 7; // // Summary: // Specifies the index number for the User-Agent HTTP header. public const int HeaderUserAgent = 39; // // Summary: // Specifies the index number for the Vary HTTP header. public const int HeaderVary = 28; // // Summary: // Specifies the index number for the Via HTTP header. public const int HeaderVia = 8; // // Summary: // Specifies the index number for the Warning HTTP header. public const int HeaderWarning = 9; // // Summary: // Specifies the index number for the WWW-Authenticate HTTP header. public const int HeaderWwwAuthenticate = 29; // // Summary: // Specifies a reason for the request. public const int ReasonCachePolicy = 2; // // Summary: // Specifies a reason for the request. public const int ReasonCacheSecurity = 3; // // Summary: // Specifies a reason for the request. public const int ReasonClientDisconnect = 4; // // Summary: // Specifies a reason for the request. The default value is System.Web.HttpWorkerRequest.ReasonResponseCacheMiss. public const int ReasonDefault = 0; // // Summary: // Specifies a reason for the request. public const int ReasonFileHandleCacheMiss = 1; // // Summary: // Specifies a reason for the request. public const int ReasonResponseCacheMiss = 0; // // Summary: // Specifies the index number for the Maximum HTTP request header. public const int RequestHeaderMaximum = 40; // // Summary: // Specifies the index number for the Maximum HTTP response header. public const int ResponseHeaderMaximum = 30; // Summary: // Initializes a new instance of the System.Web.HttpWorkerRequest class. protected HttpWorkerRequest(); // Summary: // Gets the full physical path to the Machine.config file. // // Returns: // The physical path to the Machine.config file. public virtual string MachineConfigPath { get; } // // Summary: // Gets the physical path to the directory where the ASP.NET binaries are installed. // // Returns: // The physical directory to the ASP.NET binary files. public virtual string MachineInstallDirectory { get; } // // Summary: // Gets the corresponding Event Tracking for Windows trace ID for the current // request. // // Returns: // A trace ID for the current ASP.NET request. public virtual Guid RequestTraceIdentifier { get; } // // Summary: // Gets the full physical path to the root Web.config file. // // Returns: // The physical path to the root Web.config file. public virtual string RootWebConfigPath { get; } // // Summary: // Gets a value that indicates whether asynchronous flush operations are supported. // // Returns: // true if asynchronous flush operations are supported; otherwise, false. public virtual bool SupportsAsyncFlush { get; } // // Summary: // Gets a value that indicates whether asynchronous read operations are supported. // // Returns: // true if asynchronous read operations are supported; otherwise, false. public virtual bool SupportsAsyncRead { get; } // Summary: // Sends the currently buffered response to the client. // // Parameters: // callback: // The method to call when a corresponding asynchronous operation completes. // // state: // A user-provided object that distinguishes this particular asynchronous flush // operation from other requests. // // Returns: // The worker request buffers the status, headers, and response body until an // asynchronous flush operation is initiated. If the underlying System.Web.HttpWorkerRequest // object supports asynchronous flush and this method is called from an asynchronous // module event or asynchronous handler, the send operation is performed asynchronously. // Otherwise, the implementation performs a synchronous flush operation. public virtual IAsyncResult BeginFlush(AsyncCallback callback, object state); // // Summary: // Begins an asynchronous read operation of the request entity body. // // Parameters: // buffer: // The buffer to read the data into. // // offset: // The byte offset in the buffer at which to begin writing data. // // count: // The maximum number of bytes to read. // // callback: // The method to call when a corresponding asynchronous operation completes. // // state: // A user-provided object that distinguishes this particular asynchronous read // from other requests. public virtual IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state); // // Summary: // Terminates the connection with the client. public virtual void CloseConnection(); // // Summary: // Waits for the pending asynchronous flush operation to complete. // // Parameters: // asyncResult: // A reference to the pending asynchronous request. public virtual void EndFlush(IAsyncResult asyncResult); // // Summary: // Used by the runtime to notify the System.Web.HttpWorkerRequest that request // processing for the current request is complete. public abstract void EndOfRequest(); // // Summary: // Waits for the pending asynchronous read operation to complete. // // Parameters: // asyncResult: // A reference to the pending asynchronous request. // // Returns: // The number of bytes that have been read from the stream. public virtual int EndRead(IAsyncResult asyncResult); // // Summary: // Sends all pending response data to the client. // // Parameters: // finalFlush: // true if this is the last time response data will be flushed; otherwise, false. public abstract void FlushResponse(bool finalFlush); // // Summary: // Returns the virtual path to the currently executing server application. // // Returns: // The virtual path of the current application. public virtual string GetAppPath(); // // Summary: // Returns the physical path to the currently executing server application. // // Returns: // The physical path of the current application. public virtual string GetAppPathTranslated(); // // Summary: // When overridden in a derived class, returns the application pool ID for the // current URL. // // Returns: // Always returns null. public virtual string GetAppPoolID(); // // Summary: // Gets the number of bytes read in from the client. // // Returns: // A Long containing the number of bytes read. public virtual long GetBytesRead(); // // Summary: // When overridden in a derived class, gets the certification fields (specified // in the X.509 standard) from a request issued by the client. // // Returns: // A byte array containing the stream of the entire certificate content. public virtual byte[] GetClientCertificate(); // // Summary: // Gets the certificate issuer, in binary format. // // Returns: // A byte array containing the certificate issuer expressed in binary format. public virtual byte[] GetClientCertificateBinaryIssuer(); // // Summary: // When overridden in a derived class, returns the System.Text.Encoding object // in which the client certificate was encoded. // // Returns: // The certificate encoding, expressed as an integer. public virtual int GetClientCertificateEncoding(); // // Summary: // When overridden in a derived class, gets a PublicKey object associated with // the client certificate. // // Returns: // A PublicKey object. public virtual byte[] GetClientCertificatePublicKey(); // // Summary: // When overridden in a derived class, gets the date when the certificate becomes // valid. The date varies with international settings. // // Returns: // A System.DateTime object representing when the certificate becomes valid. public virtual DateTime GetClientCertificateValidFrom(); // // Summary: // Gets the certificate expiration date. // // Returns: // A System.DateTime object representing the date that the certificate expires. public virtual DateTime GetClientCertificateValidUntil(); // // Summary: // When overridden in a derived class, returns the ID of the current connection. // // Returns: // Always returns 0. public virtual long GetConnectionID(); // // Summary: // When overridden in a derived class, returns the virtual path to the requested // URI. // // Returns: // The path to the requested URI. public virtual string GetFilePath(); // // Summary: // Returns the physical file path to the requested URI (and translates it from // virtual path to physical path: for example, "/proj1/page.aspx" to "c:\dir\page.aspx") // // Returns: // The translated physical file path to the requested URI. public virtual string GetFilePathTranslated(); // // Summary: // Returns the specified member of the request header. // // Returns: // The HTTP verb returned in the request header. public abstract string GetHttpVerbName(); // // Summary: // Provides access to the HTTP version of the request (for example, "HTTP/1.1"). // // Returns: // The HTTP version returned in the request header. public abstract string GetHttpVersion(); // // Summary: // Returns the standard HTTP request header that corresponds to the specified // index. // // Parameters: // index: // The index of the header. For example, the System.Web.HttpWorkerRequest.HeaderAllow // field. // // Returns: // The HTTP request header. public virtual string GetKnownRequestHeader(int index); // // Summary: // Returns the index number of the specified HTTP request header. // // Parameters: // header: // The name of the header. // // Returns: // The index number of the HTTP request header specified in the header parameter. public static int GetKnownRequestHeaderIndex(string header); // // Summary: // Returns the name of the specified HTTP request header. // // Parameters: // index: // The index number of the header. // // Returns: // The name of the HTTP request header specified in the index parameter. public static string GetKnownRequestHeaderName(int index); // // Summary: // Returns the index number of the specified HTTP response header. // // Parameters: // header: // The name of the HTTP header. // // Returns: // The index number of the HTTP response header specified in the header parameter. public static int GetKnownResponseHeaderIndex(string header); // // Summary: // Returns the name of the specified HTTP response header. // // Parameters: // index: // The index number of the header. // // Returns: // The name of the HTTP response header specified in the index parameter. public static string GetKnownResponseHeaderName(int index); // // Summary: // Provides access to the specified member of the request header. // // Returns: // The server IP address returned in the request header. public abstract string GetLocalAddress(); // // Summary: // Provides access to the specified member of the request header. // // Returns: // The server port number returned in the request header. public abstract int GetLocalPort(); // // Summary: // Returns additional path information for a resource with a URL extension. // That is, for the path /virdir/page.html/tail, the GetPathInfo value is /tail. // // Returns: // Additional path information for a resource. public virtual string GetPathInfo(); // // Summary: // Returns the portion of the HTTP request body that has already been read. // // Returns: // The portion of the HTTP request body that has been read. public virtual byte[] GetPreloadedEntityBody(); // // Summary: // Gets the portion of the HTTP request body that has currently been read by // using the specified buffer data and byte offset. // // Parameters: // buffer: // The data to read. // // offset: // The byte offset at which to begin reading. // // Returns: // The portion of the HTTP request body that has been read. public virtual int GetPreloadedEntityBody(byte[] buffer, int offset); // // Summary: // Gets the length of the portion of the HTTP request body that has currently // been read. // // Returns: // An integer containing the length of the currently read HTTP request body. public virtual int GetPreloadedEntityBodyLength(); // // Summary: // When overridden in a derived class, returns the HTTP protocol (HTTP or HTTPS). // // Returns: // HTTPS if the System.Web.HttpWorkerRequest.IsSecure() method is true, otherwise // HTTP. public virtual string GetProtocol(); // // Summary: // Returns the query string specified in the request URL. // // Returns: // The request query string. public abstract string GetQueryString(); // // Summary: // When overridden in a derived class, returns the response query string as // an array of bytes. // // Returns: // An array of bytes containing the response. public virtual byte[] GetQueryStringRawBytes(); // // Summary: // Returns the URL path contained in the request header with the query string // appended. // // Returns: // The raw URL path of the request header. public abstract string GetRawUrl(); // // Summary: // Provides access to the specified member of the request header. // // Returns: // The client's IP address. public abstract string GetRemoteAddress(); // // Summary: // When overridden in a derived class, returns the name of the client computer. // // Returns: // The name of the client computer. public virtual string GetRemoteName(); // // Summary: // Provides access to the specified member of the request header. // // Returns: // The client's HTTP port number. public abstract int GetRemotePort(); // // Summary: // When overridden in a derived class, returns the reason for the request. // // Returns: // Reason code. The default is ReasonResponseCacheMiss. public virtual int GetRequestReason(); // // Summary: // When overridden in a derived class, returns the name of the local server. // // Returns: // The name of the local server. public virtual string GetServerName(); // // Summary: // Returns a single server variable from a dictionary of server variables associated // with the request. // // Parameters: // name: // The name of the requested server variable. // // Returns: // The requested server variable. public virtual string GetServerVariable(string name); // // Summary: // Returns a string that describes the name of the specified HTTP status code. // // Parameters: // code: // The HTTP status code. // // Returns: // The status description. For example, System.Web.HttpWorkerRequest.GetStatusDescription(System.Int32) // (404) returns "Not Found". public static string GetStatusDescription(int code); // // Summary: // Gets the length of the entire HTTP request body. // // Returns: // An integer containing the length of the entire HTTP request body. public virtual int GetTotalEntityBodyLength(); // // Summary: // Returns a nonstandard HTTP request header value. // // Parameters: // name: // The header name. // // Returns: // The header value. public virtual string GetUnknownRequestHeader(string name); // // Summary: // Get all nonstandard HTTP header name-value pairs. // // Returns: // An array of header name-value pairs. [CLSCompliant(false)] public virtual string[][] GetUnknownRequestHeaders(); // // Summary: // Returns the virtual path to the requested URI. // // Returns: // The path to the requested URI. public abstract string GetUriPath(); // // Summary: // When overridden in a derived class, returns the context ID of the current // connection. // // Returns: // Always returns 0. public virtual long GetUrlContextID(); // // Summary: // When overridden in a derived class, returns the client's impersonation token. // // Returns: // A value representing the client's impersonation token. The default is 0. public virtual IntPtr GetUserToken(); // // Summary: // Gets the impersonation token for the request virtual path. // // Returns: // An unmanaged memory pointer for the token for the request virtual path. public virtual IntPtr GetVirtualPathToken(); // // Summary: // Returns a value indicating whether the request contains body data. // // Returns: // true if the request contains body data; otherwise, false. public bool HasEntityBody(); // // Summary: // Returns a value indicating whether HTTP response headers have been sent to // the client for the current request. // // Returns: // true if HTTP response headers have been sent to the client; otherwise, false. public virtual bool HeadersSent(); // // Summary: // Returns a value indicating whether the client connection is still active. // // Returns: // true if the client connection is still active; otherwise, false. public virtual bool IsClientConnected(); // // Summary: // Returns a value indicating whether all request data is available and no further // reads from the client are required. // // Returns: // true if all request data is available; otherwise, false. public virtual bool IsEntireEntityBodyIsPreloaded(); // // Summary: // Returns a value indicating whether the connection uses SSL. // // Returns: // true if the connection is an SSL connection; otherwise, false. The default // is false. public virtual bool IsSecure(); // // Summary: // Returns the physical path corresponding to the specified virtual path. // // Parameters: // virtualPath: // The virtual path. // // Returns: // The physical path that corresponds to the virtual path specified in the virtualPath // parameter. public virtual string MapPath(string virtualPath); // // Summary: // Reads request data from the client (when not preloaded). // // Parameters: // buffer: // The byte array to read data into. // // size: // The maximum number of bytes to read. // // Returns: // The number of bytes read. public virtual int ReadEntityBody(byte[] buffer, int size); // // Summary: // Reads request data from the client (when not preloaded) by using the specified // buffer to read from, byte offset, and maximum bytes. // // Parameters: // buffer: // The byte array to read data into. // // offset: // The byte offset at which to begin reading. // // size: // The maximum number of bytes to read. // // Returns: // The number of bytes read. public virtual int ReadEntityBody(byte[] buffer, int offset, int size); // // Summary: // Adds a Content-Length HTTP header to the response for message bodies that // are less than or equal to 2 GB. // // Parameters: // contentLength: // The length of the response, in bytes. public virtual void SendCalculatedContentLength(int contentLength); // // Summary: // Adds a Content-Length HTTP header to the response for message bodies that // are greater than 2 GB. // // Parameters: // contentLength: // The length of the response, in bytes. public virtual void SendCalculatedContentLength(long contentLength); // // Summary: // Adds a standard HTTP header to the response. // // Parameters: // index: // The header index. For example, System.Web.HttpWorkerRequest.HeaderContentLength. // // value: // The value of the header. public abstract void SendKnownResponseHeader(int index, string value); // // Summary: // Adds the contents of the specified file to the response and specifies the // starting position in the file and the number of bytes to send. // // Parameters: // handle: // The handle of the file to send. // // offset: // The starting position in the file. // // length: // The number of bytes to send. public abstract void SendResponseFromFile(IntPtr handle, long offset, long length); // // Summary: // Adds the contents of the specified file to the response and specifies the // starting position in the file and the number of bytes to send. // // Parameters: // filename: // The name of the file to send. // // offset: // The starting position in the file. // // length: // The number of bytes to send. public abstract void SendResponseFromFile(string filename, long offset, long length); // // Summary: // Adds the specified number of bytes from a byte array to the response. // // Parameters: // data: // The byte array to send. // // length: // The number of bytes to send, starting at the first byte. public abstract void SendResponseFromMemory(byte[] data, int length); // // Summary: // Adds the specified number of bytes from a block of memory to the response. // // Parameters: // data: // An unmanaged pointer to the block of memory. // // length: // The number of bytes to send. public virtual void SendResponseFromMemory(IntPtr data, int length); // // Summary: // Specifies the HTTP status code and status description of the response, such // as SendStatus(200, "Ok"). // // Parameters: // statusCode: // The status code to send // // statusDescription: // The status description to send. public abstract void SendStatus(int statusCode, string statusDescription); // // Summary: // Adds a nonstandard HTTP header to the response. // // Parameters: // name: // The name of the header to send. // // value: // The value of the header. public abstract void SendUnknownResponseHeader(string name, string value); // // Summary: // Registers for an optional notification when all the response data is sent. // // Parameters: // callback: // The notification callback that is called when all data is sent (out-of-band). // // extraData: // An additional parameter to the callback. public virtual void SetEndOfSendNotification(HttpWorkerRequest.EndOfSendNotification callback, object extraData); // Summary: // Represents the method that Notifies callers when sending of the response // is complete. // // Parameters: // wr: // The current System.Web.HttpWorkerRequest. // // extraData: // Any additional data needed to process the request. public delegate void EndOfSendNotification(HttpWorkerRequest wr, object extraData); } }
该类是一个很底层的类,主要用于asp.net服务器内部,在一些特殊情况下,可以完成一些特殊的任务,比如,上传文件时,默认情况下,asp.net会将上传的文件内容读取到服务器的内存中,然后通过控件的方式供我们使用。这种在上传小文件的时候很方便,大文件的时候,服务器内存将出现问题,导致浏览器不能将文件继续传输。浏览器将显示“The page cannot be displayed -cannot find server or dns error.”,这个错误无法在服务器端进行捕获或者处理。
借助HttpWorkerRequest的GetPreloadedEntityBody和ReadEntityBody方法,我们可以自己分块从客户端读取上传的文件内容。然后将内容保存在一个临时文件中.
SlickUpload组件,就是基于这个实现的。感兴趣的可以看看这篇文章,如何使用该组件。
http://blog.csdn.net/zhoufoxcn/article/details/5765748
下载源码
-
博客地址:http://www.cnblogs.com/wolf-sun/
博客版权:如果文中有不妥或者错误的地方还望高手的你指出,以免误人子弟。如果觉得本文对你有所帮助不如【推荐】一下!如果你有更好的建议,不如留言一起讨论,共同进步! 再次感谢您耐心的读完本篇文章。