WebHeaderCollection.IsRestricted 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
測試是否可設定指定的 HTTP 標頭。
多載
IsRestricted(String) |
測試是否可為要求設定指定的 HTTP 標頭。 |
IsRestricted(String, Boolean) |
測試是否可以設定要求或回應之指定的 HTTP 標頭。 |
IsRestricted(String)
測試是否可為要求設定指定的 HTTP 標頭。
public:
static bool IsRestricted(System::String ^ headerName);
public static bool IsRestricted (string headerName);
static member IsRestricted : string -> bool
Public Shared Function IsRestricted (headerName As String) As Boolean
參數
- headerName
- String
要測試的標頭。
傳回
如果標頭是受限制的,則為 true
,否則為 false
。
例外狀況
headerName
是 null
或 Empty。
headerName
包含無效的字元。
範例
下列範例會 IsRestricted 檢查 屬性,以查看是否禁止設定任何標頭。
try
{
// Create a web request for S"www.msn.com".
HttpWebRequest^ myHttpWebRequest = dynamic_cast<HttpWebRequest^>(WebRequest::Create( "http://www.msn.com" ));
// Get the associated response for the above request.
HttpWebResponse^ myHttpWebResponse = dynamic_cast<HttpWebResponse^>(myHttpWebRequest->GetResponse());
// Get the headers associated with the response.
WebHeaderCollection^ myWebHeaderCollection = myHttpWebResponse->Headers;
for ( int i = 0; i < myWebHeaderCollection->Count; i++ )
{
// Check if the first response header is restriced.
if ( WebHeaderCollection::IsRestricted( dynamic_cast<String^>(myWebHeaderCollection->AllKeys[ i ]) ) )
Console::WriteLine( "' {0}' is a restricted header", myWebHeaderCollection->AllKeys[ i ] );
else
Console::WriteLine( "' {0}' is not a restricted header", myWebHeaderCollection->AllKeys[ i ] );
}
myHttpWebResponse->Close();
}
catch ( WebException^ e )
{
Console::WriteLine( "\nWebException is thrown.\nMessage is: {0}", e->Message );
if ( e->Status == WebExceptionStatus::ProtocolError )
{
Console::WriteLine( "Status Code : {0}", (dynamic_cast<HttpWebResponse^>(e->Response))->StatusCode );
Console::WriteLine( "Status Description : {0}", (dynamic_cast<HttpWebResponse^>(e->Response))->StatusDescription );
Console::WriteLine( "Server : {0}", (dynamic_cast<HttpWebResponse^>(e->Response))->Server );
}
}
catch ( Exception^ e )
{
Console::WriteLine( "Exception is thrown. Message is : {0}", e->Message );
}
try {
// Create a web request for "www.msn.com".
HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create("http://www.msn.com");
// Get the associated response for the above request.
HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();
// Get the headers associated with the response.
WebHeaderCollection myWebHeaderCollection = myHttpWebResponse.Headers;
for (int i = 0; i < myWebHeaderCollection.Count;i++)
{
// Check if the first response header is restricted.
if(WebHeaderCollection.IsRestricted(myWebHeaderCollection.AllKeys[i]))
Console.WriteLine("'{0}' is a restricted header", myWebHeaderCollection.AllKeys[i]);
else
Console.WriteLine("'{0}' is not a restricted header", myWebHeaderCollection.AllKeys[i]);
}
myHttpWebResponse.Close();
}
catch(WebException e) {
Console.WriteLine("\nWebException is thrown.\nMessage is:" + e.Message);
if(e.Status == WebExceptionStatus.ProtocolError) {
Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
Console.WriteLine("Server : {0}", ((HttpWebResponse)e.Response).Server);
}
}
catch(Exception e) {
Console.WriteLine("Exception is thrown. Message is :" + e.Message);
}
}
Public Shared Sub Main()
Try
'Create a web request for "www.msn.com".
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("http://www.msn.com"), HttpWebRequest)
'Get the associated response for the above request.
Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
'Get the headers associated with the response.
Dim myWebHeaderCollection As WebHeaderCollection = myHttpWebResponse.Headers
'Check if the first response header is restricted.
dim i as integer
for i =0 to myWebHeaderCollection.Count-1
If WebHeaderCollection.IsRestricted(myWebHeaderCollection.AllKeys(i)) Then
Console.WriteLine("'{0}' is a restricted header", myWebHeaderCollection.AllKeys(i))
Else
Console.WriteLine("'{0}' is not a restricted header", myWebHeaderCollection.AllKeys(i))
End If
next
myHttpWebResponse.Close()
Catch e As WebException
Console.WriteLine(e.Message)
If e.Status = WebExceptionStatus.ProtocolError Then
Console.WriteLine("Status Code : {0}", CType(e.Response, HttpWebResponse).StatusCode)
Console.WriteLine("Status Description : {0}", CType(e.Response, HttpWebResponse).StatusDescription)
Console.WriteLine("Server : {0}", CType(e.Response, HttpWebResponse).Server)
End If
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub
備註
方法 IsRestricted 會傳 true
回 ,指出標頭受到限制,而且必須使用屬性來設定,而不是直接設定,或由系統設定。 受限制的標頭如下:
接受
連線
Content-Length
Content-Type
Date
Expect
Host
If-Modified-Since
範圍
Referer
Transfer-Encoding
User-Agent
Proxy-Connection
適用於
IsRestricted(String, Boolean)
測試是否可以設定要求或回應之指定的 HTTP 標頭。
public:
static bool IsRestricted(System::String ^ headerName, bool response);
public static bool IsRestricted (string headerName, bool response);
static member IsRestricted : string * bool -> bool
Public Shared Function IsRestricted (headerName As String, response As Boolean) As Boolean
參數
- headerName
- String
要測試的標頭。
- response
- Boolean
true
測試回應; false
測試要求。
傳回
如果標頭是受限制的,則為 true
,否則為 false
。
例外狀況
headerName
是 null
或 Empty。
headerName
包含無效的字元。
範例
下列範例會 IsRestricted 檢查 屬性,以查看是否禁止設定任何要求標頭。
try
{
// Create a web request for S"www.msn.com".
HttpWebRequest^ myHttpWebRequest = dynamic_cast<HttpWebRequest^>(WebRequest::Create( "http://www.msn.com" ));
// Get the associated response for the above request.
HttpWebResponse^ myHttpWebResponse = dynamic_cast<HttpWebResponse^>(myHttpWebRequest->GetResponse());
// Get the headers associated with the response.
WebHeaderCollection^ myWebHeaderCollection = myHttpWebResponse->Headers;
for ( int i = 0; i < myWebHeaderCollection->Count; i++ )
{
// Check if the first response header is restriced.
if ( WebHeaderCollection::IsRestricted( dynamic_cast<String^>(myWebHeaderCollection->AllKeys[ i ]) ) )
Console::WriteLine( "' {0}' is a restricted header", myWebHeaderCollection->AllKeys[ i ] );
else
Console::WriteLine( "' {0}' is not a restricted header", myWebHeaderCollection->AllKeys[ i ] );
}
myHttpWebResponse->Close();
}
catch ( WebException^ e )
{
Console::WriteLine( "\nWebException is thrown.\nMessage is: {0}", e->Message );
if ( e->Status == WebExceptionStatus::ProtocolError )
{
Console::WriteLine( "Status Code : {0}", (dynamic_cast<HttpWebResponse^>(e->Response))->StatusCode );
Console::WriteLine( "Status Description : {0}", (dynamic_cast<HttpWebResponse^>(e->Response))->StatusDescription );
Console::WriteLine( "Server : {0}", (dynamic_cast<HttpWebResponse^>(e->Response))->Server );
}
}
catch ( Exception^ e )
{
Console::WriteLine( "Exception is thrown. Message is : {0}", e->Message );
}
try {
// Create a web request for "www.msn.com".
HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create("http://www.msn.com");
// Get the associated response for the above request.
HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();
// Get the headers associated with the response.
WebHeaderCollection myWebHeaderCollection = myHttpWebResponse.Headers;
for (int i = 0; i < myWebHeaderCollection.Count;i++)
{
// Check if the first response header is restricted.
if(WebHeaderCollection.IsRestricted(myWebHeaderCollection.AllKeys[i]))
Console.WriteLine("'{0}' is a restricted header", myWebHeaderCollection.AllKeys[i]);
else
Console.WriteLine("'{0}' is not a restricted header", myWebHeaderCollection.AllKeys[i]);
}
myHttpWebResponse.Close();
}
catch(WebException e) {
Console.WriteLine("\nWebException is thrown.\nMessage is:" + e.Message);
if(e.Status == WebExceptionStatus.ProtocolError) {
Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
Console.WriteLine("Server : {0}", ((HttpWebResponse)e.Response).Server);
}
}
catch(Exception e) {
Console.WriteLine("Exception is thrown. Message is :" + e.Message);
}
}
Public Shared Sub Main()
Try
'Create a web request for "www.msn.com".
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("http://www.msn.com"), HttpWebRequest)
'Get the associated response for the above request.
Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
'Get the headers associated with the response.
Dim myWebHeaderCollection As WebHeaderCollection = myHttpWebResponse.Headers
'Check if the first response header is restricted.
dim i as integer
for i =0 to myWebHeaderCollection.Count-1
If WebHeaderCollection.IsRestricted(myWebHeaderCollection.AllKeys(i)) Then
Console.WriteLine("'{0}' is a restricted header", myWebHeaderCollection.AllKeys(i))
Else
Console.WriteLine("'{0}' is not a restricted header", myWebHeaderCollection.AllKeys(i))
End If
next
myHttpWebResponse.Close()
Catch e As WebException
Console.WriteLine(e.Message)
If e.Status = WebExceptionStatus.ProtocolError Then
Console.WriteLine("Status Code : {0}", CType(e.Response, HttpWebResponse).StatusCode)
Console.WriteLine("Status Description : {0}", CType(e.Response, HttpWebResponse).StatusDescription)
Console.WriteLine("Server : {0}", CType(e.Response, HttpWebResponse).Server)
End If
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub
備註
方法 IsRestricted 會傳 true
回 ,指出要求或響應標頭受到限制,而且必須使用屬性來設定,而不是直接設定或由系統設定。 受限制的標頭如下:
接受
連線
Content-Length
Content-Type
Date
Expect
Host
If-Modified-Since
範圍
Referer
Transfer-Encoding
User-Agent
Proxy-Connection