Skip to content

Commit a48dd56

Browse files
rzikmliveans
andauthored
Scrub references to WebRequest from examples (#8467)
* Remore reference to WebRequest in System.Net ns doc * Scrub from Uri * HttpListener * HttpStatusCode * HttpVersion * WebProxy * NetworkCredential * SslStream * Fix compilation * Add missing projects * Fix compilation * Another compilation fix * Final compilation fix * Update snippets/cpp/VS_Snippets_Remoting/HttpVersion_Version10/CPP/httpversion_version10.cpp Co-authored-by: Ahmet İbrahim AKSOY <[email protected]> Co-authored-by: Ahmet İbrahim AKSOY <[email protected]>
1 parent 08da320 commit a48dd56

File tree

30 files changed

+226
-239
lines changed

30 files changed

+226
-239
lines changed

snippets/cpp/VS_Snippets_Remoting/Classic HttpStatusCode Example/CPP/source.cpp

Lines changed: 0 additions & 30 deletions
This file was deleted.

snippets/cpp/VS_Snippets_Remoting/Classic NetworkCredential Example/CPP/source.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using namespace System;
66
using namespace System::Net;
7+
using namespace System::Net::Http;
78
using namespace System::IO;
89
using namespace System::Windows::Forms;
910

@@ -22,11 +23,14 @@ public ref class Form1: public Form
2223

2324
CredentialCache^ myCache = gcnew CredentialCache;
2425

25-
myCache->Add( gcnew Uri( "www.contoso.com" ), "Basic", myCred );
26-
myCache->Add( gcnew Uri( "app.contoso.com" ), "Basic", myCred );
26+
myCache->Add( gcnew Uri( "http://www.contoso.com" ), "Basic", myCred );
27+
myCache->Add( gcnew Uri( "http://app.contoso.com" ), "Basic", myCred );
2728

28-
WebRequest^ wr = WebRequest::Create( "www.contoso.com" );
29-
wr->Credentials = myCache;
29+
// HttpClient lifecycle management best practices:
30+
// https://learn.microsoft.com/dotnet/fundamentals/networking/http/httpclient-guidelines#recommended-use
31+
HttpClientHandler^ handler = gcnew HttpClientHandler();
32+
handler->Credentials = myCache;
33+
HttpClient^ client = gcnew HttpClient(handler);
3034
// </Snippet1>
3135
}
3236
};

snippets/cpp/VS_Snippets_Remoting/Classic Uri Example/CPP/source.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using namespace System;
99
using namespace System::Data;
1010
using namespace System::Net;
11+
using namespace System::Net::Http;
1112
using namespace System::Windows::Forms;
1213

1314
public ref class Form1: public Form
@@ -16,8 +17,13 @@ public ref class Form1: public Form
1617
void Method()
1718
{
1819
// <Snippet1>
19-
Uri^ siteUri = gcnew Uri( "http://www.contoso.com/" );
20-
WebRequest^ wr = WebRequest::Create( siteUri );
20+
Uri^ siteUri = gcnew Uri("http://www.contoso.com/");
21+
22+
// HttpClient lifecycle management best practices:
23+
// https://learn.microsoft.com/dotnet/fundamentals/networking/http/httpclient-guidelines#recommended-use
24+
HttpClient^ client = gcnew HttpClient;
25+
HttpRequestMessage^ request = gcnew HttpRequestMessage(HttpMethod::Get, siteUri);
26+
HttpResponseMessage^ response = client->Send(request);
2127
// </Snippet1>
2228
}
2329
};

snippets/cpp/VS_Snippets_Remoting/Classic WebProxy Example/CPP/source.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@
22

33
using namespace System;
44
using namespace System::Net;
5+
using namespace System::Net::Http;
56

67
public ref class Sample
78
{
89
private:
910
void sampleFunction()
1011
{
1112
// <Snippet1>
12-
WebProxy^ proxyObject = gcnew WebProxy( "http://proxyserver:80/",true );
13-
WebRequest^ req = WebRequest::Create( "http://www.contoso.com" );
14-
req->Proxy = proxyObject;
13+
WebProxy^ proxyObject = gcnew WebProxy("http://proxyserver:80/", true);
14+
15+
// HttpClient lifecycle management best practices:
16+
// https://learn.microsoft.com/dotnet/fundamentals/networking/http/httpclient-guidelines#recommended-use
17+
HttpClientHandler^ handler = gcnew HttpClientHandler();
18+
handler->Proxy = proxyObject;
19+
HttpClient^ client = gcnew HttpClient(handler);
1520
// </Snippet1>
1621
}
1722
};
Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,49 @@
11
/*System::Net::HttpVersion::Version10
22
This program demonstrates the 'Version10' field of the 'HttpVersion' Class.
3-
The 'ProtocolVersion' property of 'HttpWebrequest' class identifies the Version of the Protocol being used.
4-
A new 'HttpWebRequest' Object* is created.
5-
Then the default value of 'ProtocolVersion' property is displayed to the console.
6-
The 'Version10' field of the 'HttpVersion' class is assigned to the 'ProtocolVersion' property of the 'HttpWebRequest' Class.
7-
The changed Version and the 'ProtocolVersion' of the response Object* are displayed.
3+
The 'Version' property of 'HttpRequestMessage' class identifies the Version of HTTP being used.
4+
Then the default value of 'Version' property is displayed to the console.
5+
The 'Version10' field of the 'HttpVersion' class is assigned to the 'Version' property of the 'HttpRequestMessage' Class.
6+
The changed Version and the 'Version' of the response object are displayed.
87
*/
98

109
#using <System.dll>
1110

1211
using namespace System;
1312
using namespace System::IO;
1413
using namespace System::Net;
14+
using namespace System::Net::Http;
1515

1616
int main()
1717
{
1818
try
1919
{
20-
// <Snippet1>
21-
// Create a 'HttpWebRequest' object.
22-
HttpWebRequest^ myHttpWebRequest = (HttpWebRequest^)( WebRequest::Create( "http://www.microsoft.com" ) );
23-
Console::WriteLine( "\nThe 'ProtocolVersion' of the protocol before assignment is : {0}", myHttpWebRequest->ProtocolVersion );
24-
// Assign Version10 to ProtocolVersion.
25-
myHttpWebRequest->ProtocolVersion = HttpVersion::Version10;
26-
// Assign the response Object* of 'HttpWebRequest' to a 'HttpWebResponse' variable.
27-
HttpWebResponse^ myHttpWebResponse = (HttpWebResponse^)( myHttpWebRequest->GetResponse() );
28-
Console::WriteLine( "\nThe 'ProtocolVersion' of the protocol after assignment is : {0}", myHttpWebRequest->ProtocolVersion );
29-
Console::WriteLine( "\nThe 'ProtocolVersion' of the response Object* is : {0}", myHttpWebResponse->ProtocolVersion );
30-
// </Snippet1>
31-
Console::WriteLine( "\nPress 'Enter' Key to Continue.............." );
20+
// <Snippet1>
21+
// HttpClient lifecycle management best practices:
22+
// https://learn.microsoft.com/dotnet/fundamentals/networking/http/httpclient-guidelines#recommended-use
23+
HttpClient^ client = gcnew HttpClient();
24+
25+
HttpRequestMessage^ request = new HttpRequestMessage(HttpMethod::Get, "http://www.microsoft.com");
26+
Console::WriteLine("Default HTTP request version is {0}", request.Version);
27+
28+
request.Version = HttpVersion.Version10;
29+
Console::WriteLine("Request version after assignment is {0}", request.Version);
30+
31+
HttpResponseMessage^ response = client->Send(request);
32+
Console::WriteLine("Response HTTP version {0}", response.Version);
33+
// </Snippet1>
34+
Console::WriteLine("\nPress 'Enter' Key to Continue..............");
3235
Console::Read();
3336
}
34-
catch ( WebException^ e )
37+
catch (HttpRequestException^ e)
3538
{
36-
Console::WriteLine( "\nWebException Caught!" );
37-
Console::WriteLine( "Message : {0} ", e->Message );
38-
Console::WriteLine( "Source : {0} ", e->Source );
39+
Console::WriteLine("HttpRequestException Caught!");
40+
Console::WriteLine("Message : {0} ", e->Message);
41+
Console::WriteLine("Source : {0} ", e->Source);
3942
}
40-
catch ( Exception^ e )
43+
catch (Exception^ e)
4144
{
42-
Console::WriteLine( "\nException Caught!" );
43-
Console::WriteLine( "Source : {0}", e->Source );
44-
Console::WriteLine( "Message : {0}", e->Message );
45+
Console::WriteLine("Exception Caught!");
46+
Console::WriteLine("Source : {0}", e->Source);
47+
Console::WriteLine("Message : {0}", e->Message);
4548
}
4649
}

snippets/csharp/System.Net/HttpStatusCode/Overview/source.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.

snippets/csharp/System.Net/HttpVersion/Overview/httpversion_version10.cs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/*System.Net.HttpVersion.Version10
22
This program demonstrates the 'Version10' field of the 'HttpVersion' Class.
3-
The 'ProtocolVersion' property of 'HttpWebrequest' class identifies the Version of the Protocol being used.
4-
A new 'HttpWebRequest' object is created.
5-
Then the default value of 'ProtocolVersion' property is displayed to the console.
6-
The 'Version10' field of the 'HttpVersion' class is assigned to the 'ProtocolVersion' property of the 'HttpWebRequest' Class.
7-
The changed Version and the 'ProtocolVersion' of the response object are displayed.
3+
The 'Version' property of 'HttpRequestMessage' class identifies the Version of HTTP being used.
4+
Then the default value of 'Version' property is displayed to the console.
5+
The 'Version10' field of the 'HttpVersion' class is assigned to the 'Version' property of the 'HttpRequestMessage' Class.
6+
The changed Version and the 'Version' of the response object are displayed.
87
*/
98

109
using System;
1110
using System.IO;
1211
using System.Net;
12+
using System.Net.Http;
1313

1414
class HttpVersion_Version10
1515
{
@@ -18,28 +18,31 @@ public static void Main()
1818
try
1919
{
2020
// <Snippet1>
21-
// Create a 'HttpWebRequest' object.
22-
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
23-
Console.WriteLine("\nThe 'ProtocolVersion' of the protocol before assignment is :{0}",myHttpWebRequest.ProtocolVersion);
24-
// Assign Version10 to ProtocolVersion.
25-
myHttpWebRequest.ProtocolVersion=HttpVersion.Version10;
26-
// Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
27-
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
28-
Console.WriteLine("\nThe 'ProtocolVersion' of the protocol after assignment is :{0}",myHttpWebRequest.ProtocolVersion);
29-
Console.WriteLine("\nThe 'ProtocolVersion' of the response object is :{0}",myHttpWebResponse.ProtocolVersion);
21+
// HttpClient lifecycle management best practices:
22+
// https://learn.microsoft.com/dotnet/fundamentals/networking/http/httpclient-guidelines#recommended-use
23+
HttpClient client = new HttpClient();
24+
25+
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://www.microsoft.com");
26+
Console.WriteLine("Default HTTP request version is {0}", request.Version);
27+
28+
request.Version = HttpVersion.Version10;
29+
Console.WriteLine("Request version after assignment is {0}", request.Version);
30+
31+
HttpResponseMessage response = client.Send(request);
32+
Console.WriteLine("Response HTTP version {0}", response.Version);
3033
// </Snippet1>
3134
Console.WriteLine("\nPress 'Enter' Key to Continue..............");
3235
Console.Read();
3336
}
34-
catch(WebException e)
37+
catch(HttpRequestException e)
3538
{
36-
Console.WriteLine("\nWebException Caught!");
39+
Console.WriteLine("HttpRequestException Caught!");
3740
Console.WriteLine("Message :{0} ",e.Message);
3841
Console.WriteLine("Source :{0} ",e.Source);
3942
}
4043
catch(Exception e)
4144
{
42-
Console.WriteLine("\nException Caught!");
45+
Console.WriteLine("Exception Caught!");
4346
Console.WriteLine("Source :{0}" , e.Source);
4447
Console.WriteLine("Message :{0}" , e.Message);
4548
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Library</OutputType>
5+
<TargetFrameworks>net6.0</TargetFrameworks>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using System;
22
using System.Net;
3+
using System.Net.Http;
34
using System.IO;
4-
using System.Windows.Forms;
55

6-
public class Form1: Form
6+
public class Program
77
{
8-
public void Method()
8+
public static void Main(string[] args)
99
{
1010
string SecurelyStoredUserName = "";
1111
string SecurelyStoredPassword = "";
@@ -16,12 +16,15 @@ public void Method()
1616

1717
CredentialCache myCache = new CredentialCache();
1818

19-
myCache.Add(new Uri("www.contoso.com"), "Basic", myCred);
20-
myCache.Add(new Uri("app.contoso.com"), "Basic", myCred);
21-
22-
WebRequest wr = WebRequest.Create("www.contoso.com");
23-
wr.Credentials = myCache;
19+
myCache.Add(new Uri("http://www.contoso.com"), "Basic", myCred);
20+
myCache.Add(new Uri("http://app.contoso.com"), "Basic", myCred);
2421

22+
// HttpClient lifecycle management best practices:
23+
// https://learn.microsoft.com/dotnet/fundamentals/networking/http/httpclient-guidelines#recommended-use
24+
HttpClient client = new HttpClient(new HttpClientHandler
25+
{
26+
Credentials = myCache
27+
});
2528
// </Snippet1>
2629
}
2730
}
Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
using System;
22
using System.Net;
3+
using System.Net.Http;
34

4-
public class Sample {
5+
public class Sample
6+
{
57

6-
private void sampleFunction() {
7-
// <Snippet1>
8-
WebProxy proxyObject = new WebProxy("http://proxyserver:80/",true);
9-
WebRequest req = WebRequest.Create("http://www.contoso.com");
10-
req.Proxy = proxyObject;
8+
private void sampleFunction()
9+
{
10+
// <Snippet1>
11+
WebProxy proxyObject = new WebProxy("http://proxyserver:80/", true);
1112

12-
// </Snippet1>
13-
}
13+
// HttpClient lifecycle management best practices:
14+
// https://learn.microsoft.com/dotnet/fundamentals/networking/http/httpclient-guidelines#recommended-use
15+
HttpClient client = new HttpClient(new HttpClientHandler
16+
{
17+
Proxy = proxyObject
18+
});
19+
// </Snippet1>
20+
}
1421
}

0 commit comments

Comments
 (0)