@@ -9,29 +9,26 @@ class HttpClient_Example
99// <Snippet1>
1010 static async Task Main ( )
1111 {
12- // Create a New HttpClient object.
13- HttpClient client = new HttpClient ( ) ;
14-
15- // Call asynchronous network methods in a try/catch block to handle exceptions
16- try
12+ // Create a New HttpClient object and dispose it when done, so the app doesn't leak resources
13+ using ( HttpClient client = new HttpClient ( ) )
1714 {
18- HttpResponseMessage response = await client . GetAsync ( "http://www.contoso.com/" ) ;
19- response . EnsureSuccessStatusCode ( ) ;
20- string responseBody = await response . Content . ReadAsStringAsync ( ) ;
21- // Above three lines can be replaced with new helper method below
22- // string responseBody = await client.GetStringAsync(uri);
15+ // Call asynchronous network methods in a try/catch block to handle exceptions
16+ try
17+ {
18+ HttpResponseMessage response = await client . GetAsync ( "http://www.contoso.com/" ) ;
19+ response . EnsureSuccessStatusCode ( ) ;
20+ string responseBody = await response . Content . ReadAsStringAsync ( ) ;
21+ // Above three lines can be replaced with new helper method below
22+ // string responseBody = await client.GetStringAsync(uri);
2323
24- Console . WriteLine ( responseBody ) ;
25- }
26- catch ( HttpRequestException e )
27- {
28- Console . WriteLine ( "\n Exception Caught!" ) ;
29- Console . WriteLine ( "Message :{0} " , e . Message ) ;
24+ Console . WriteLine ( responseBody ) ;
25+ }
26+ catch ( HttpRequestException e )
27+ {
28+ Console . WriteLine ( "\n Exception Caught!" ) ;
29+ Console . WriteLine ( "Message :{0} " , e . Message ) ;
30+ }
3031 }
31-
32- // Need to call dispose on the HttpClient object
33- // when done using it, so the app doesn't leak resources
34- client . Dispose ( true ) ;
3532 }
3633// </Snippet1>
3734}
0 commit comments