Skip to content

Commit 1e2dc45

Browse files
authored
Merge pull request #768 from dotnet/master
Update live with current master
2 parents ed18bbe + fd74e20 commit 1e2dc45

File tree

4 files changed

+156
-0
lines changed

4 files changed

+156
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.IO;
3+
using System.Net;
4+
5+
namespace Examples.System.Net
6+
{
7+
public class WebRequestGetExample
8+
{
9+
public static void Main()
10+
{
11+
// Create a request for the URL.
12+
WebRequest request = WebRequest.Create(
13+
"http://www.contoso.com/default.html");
14+
// If required by the server, set the credentials.
15+
request.Credentials = CredentialCache.DefaultCredentials;
16+
// Get the response.
17+
WebResponse response = request.GetResponse();
18+
// Display the status.
19+
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
20+
// Get the stream containing content returned by the server.
21+
// The using block ensures the stream is automatically closed.
22+
using (Stream dataStream = response.GetResponseStream())
23+
{
24+
// Open the stream using a StreamReader for easy access.
25+
StreamReader reader = new StreamReader(dataStream);
26+
// Read the content.
27+
string responseFromServer = reader.ReadToEnd();
28+
// Display the content.
29+
Console.WriteLine(responseFromServer);
30+
}
31+
// Close the response.
32+
response.Close();
33+
}
34+
}
35+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.IO;
3+
using System.Net;
4+
using System.Text;
5+
6+
namespace Examples.System.Net
7+
{
8+
public class WebRequestPostExample
9+
{
10+
public static void Main()
11+
{
12+
// Create a request using a URL that can receive a post.
13+
WebRequest request = WebRequest.Create("http://www.contoso.com/PostAccepter.aspx ");
14+
// Set the Method property of the request to POST.
15+
request.Method = "POST";
16+
// Create POST data and convert it to a byte array.
17+
string postData = "This is a test that posts this string to a Web server.";
18+
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
19+
// Set the ContentType property of the WebRequest.
20+
request.ContentType = "application/x-www-form-urlencoded";
21+
// Set the ContentLength property of the WebRequest.
22+
request.ContentLength = byteArray.Length;
23+
// Get the request stream.
24+
Stream dataStream = request.GetRequestStream();
25+
// Write the data to the request stream.
26+
dataStream.Write(byteArray, 0, byteArray.Length);
27+
// Close the Stream object.
28+
dataStream.Close();
29+
// Get the response.
30+
WebResponse response = request.GetResponse();
31+
// Display the status.
32+
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
33+
// Get the stream containing content returned by the server.
34+
// The using block ensures the stream is automatically closed.
35+
using (dataStream = response.GetResponseStream())
36+
{
37+
// Open the stream using a StreamReader for easy access.
38+
StreamReader reader = new StreamReader(dataStream);
39+
// Read the content.
40+
string responseFromServer = reader.ReadToEnd();
41+
// Display the content.
42+
Console.WriteLine(responseFromServer);
43+
}
44+
// Close the response.
45+
response.Close();
46+
}
47+
}
48+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Imports System.IO
2+
Imports System.Net
3+
4+
Namespace Examples.System.Net
5+
Public Class WebRequestGetExample
6+
Public Shared Sub Main()
7+
' Create a request for the URL.
8+
Dim request As WebRequest =
9+
WebRequest.Create("http://www.contoso.com/default.html")
10+
' If required by the server, set the credentials.
11+
request.Credentials = CredentialCache.DefaultCredentials
12+
' Get the response.
13+
Dim response As WebResponse = request.GetResponse()
14+
' Display the status.
15+
Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
16+
' Get the stream containing content returned by the server.
17+
' The using block ensures the stream is automatically closed.
18+
Using dataStream As Stream = response.GetResponseStream()
19+
' Open the stream using a StreamReader for easy access.
20+
Dim reader As New StreamReader(dataStream)
21+
' Read the content.
22+
Dim responseFromServer As String = reader.ReadToEnd()
23+
' Display the content.
24+
Console.WriteLine(responseFromServer)
25+
End Using
26+
' Clean up the response.
27+
response.Close()
28+
End Sub
29+
End Class
30+
End Namespace
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Imports System.IO
2+
Imports System.Net
3+
Imports System.Text
4+
5+
Namespace Examples.System.Net
6+
Public Class WebRequestPostExample
7+
Public Shared Sub Main()
8+
' Create a request using a URL that can receive a post.
9+
Dim request As WebRequest = WebRequest.Create("http://www.contoso.com/PostAccepter.aspx ")
10+
' Set the Method property of the request to POST.
11+
request.Method = "POST"
12+
' Create POST data and convert it to a byte array.
13+
Dim postData As String = "This is a test that posts this string to a Web server."
14+
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
15+
' Set the ContentType property of the WebRequest.
16+
request.ContentType = "application/x-www-form-urlencoded"
17+
' Set the ContentLength property of the WebRequest.
18+
request.ContentLength = byteArray.Length
19+
' Get the request stream.
20+
Dim dataStream As Stream = request.GetRequestStream()
21+
' Write the data to the request stream.
22+
dataStream.Write(byteArray, 0, byteArray.Length)
23+
' Close the Stream object.
24+
dataStream.Close()
25+
' Get the response.
26+
Dim response As WebResponse = request.GetResponse()
27+
' Display the status.
28+
Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
29+
' Get the stream containing content returned by the server.
30+
' The using block ensures the stream is automatically closed.
31+
Using dataStream1 As Stream = response.GetResponseStream()
32+
' Open the stream using a StreamReader for easy access.
33+
Dim reader As New StreamReader(dataStream1)
34+
' Read the content.
35+
Dim responseFromServer As String = reader.ReadToEnd()
36+
' Display the content.
37+
Console.WriteLine(responseFromServer)
38+
End Using
39+
' Clean up the response.
40+
response.Close()
41+
End Sub
42+
End Class
43+
End Namespace

0 commit comments

Comments
 (0)