HttpWebRequest.ProtocolVersion 属性

定义

获取或设置用于请求的 HTTP 版本。

public:
 property Version ^ ProtocolVersion { Version ^ get(); void set(Version ^ value); };
public Version ProtocolVersion { get; set; }
member this.ProtocolVersion : Version with get, set
Public Property ProtocolVersion As Version

属性值

用于请求的 HTTP 版本。 默认值为 Version11

例外

HTTP 版本设置为 1.0 或 1.1 以外的值。

示例

下面的代码示例设置 ProtocolVersion Property。

// Create a new 'HttpWebRequest' Object to the mentioned URL.
HttpWebRequest^ myHttpWebRequest = (HttpWebRequest^)( WebRequest::Create( "http://www.microsoft.com" ) );
// Use the existing 'ProtocolVersion' , and display it onto the console.
Console::WriteLine( "\nThe 'ProtocolVersion' of the protocol used is {0}", myHttpWebRequest->ProtocolVersion );
// Set the 'ProtocolVersion' property of the 'HttpWebRequest' to 'Version1::0' .
myHttpWebRequest->ProtocolVersion = HttpVersion::Version10;
// Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
HttpWebResponse^ myHttpWebResponse = (HttpWebResponse^)( myHttpWebRequest->GetResponse() );
Console::WriteLine( "\nThe 'ProtocolVersion' of the protocol changed to {0}", myHttpWebRequest->ProtocolVersion );
Console::WriteLine( "\nThe protocol version of the response Object* is {0}", myHttpWebResponse->ProtocolVersion );
// Create a new 'HttpWebRequest' Object to the mentioned URL.
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
// Use the existing 'ProtocolVersion' , and display it onto the console.	
Console.WriteLine("\nThe 'ProtocolVersion' of the protocol used is {0}",myHttpWebRequest.ProtocolVersion);
// Set the 'ProtocolVersion' property of the 'HttpWebRequest' to 'Version1.0' .
myHttpWebRequest.ProtocolVersion=HttpVersion.Version10;
 // Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
 HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
Console.WriteLine("\nThe 'ProtocolVersion' of the protocol changed to {0}",myHttpWebRequest.ProtocolVersion);
Console.WriteLine("\nThe protocol version of the response object is {0}",myHttpWebResponse.ProtocolVersion);
' Create a new 'HttpWebRequest' Object to the mentioned URL.
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("http://www.microsoft.com"), HttpWebRequest)
' Use the existing 'ProtocolVersion' , and display it onto the console.	
Console.WriteLine(ControlChars.Cr + "The 'ProtocolVersion' of the protocol used is {0}", myHttpWebRequest.ProtocolVersion)
' Set the 'ProtocolVersion' property of the 'HttpWebRequest' to 'Version1.0' .
myHttpWebRequest.ProtocolVersion = HttpVersion.Version10
'  Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
Console.WriteLine(ControlChars.Cr + "The 'ProtocolVersion' of the protocol changed to {0}", myHttpWebRequest.ProtocolVersion)
Console.WriteLine(ControlChars.Cr + "The protocol version of the response object is {0}", myHttpWebResponse.ProtocolVersion)

注解

HttpWebRequest 仅支持 HTTP 的 1.0 和 1.1 版。 将 设置为 ProtocolVersion 其他版本会引发异常。

注意

若要设置当前请求的 HTTP 版本,请使用 Version10 类的 HttpVersionVersion11 字段。

适用于