HttpWebRequest.ProtocolVersion Property

Definition

Gets or sets the version of HTTP to use for the request.

C#
public Version ProtocolVersion { get; set; }

Property Value

The HTTP version to use for the request. The default is Version11.

Exceptions

The HTTP version is set to a value other than 1.0 or 1.1.

Examples

The following code example sets the ProtocolVersion Property.

C#
// 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);

Remarks

Caution

WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete, and you shouldn't use them for new development. Use HttpClient instead.

The HttpWebRequest class supports only versions 1.0 and 1.1 of HTTP. Setting ProtocolVersion to a different version throws an exception.

Note

To set the HTTP version of the current request, use the Version10 and Version11 fields of the HttpVersion class.

Applies to

Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1