diff --git a/snippets/csharp/VS_Snippets_Network/RequestDataUsingWebRequest/cs/WebRequestGetExample.cs b/snippets/csharp/VS_Snippets_Network/RequestDataUsingWebRequest/cs/WebRequestGetExample.cs new file mode 100644 index 00000000000..938a9aec711 --- /dev/null +++ b/snippets/csharp/VS_Snippets_Network/RequestDataUsingWebRequest/cs/WebRequestGetExample.cs @@ -0,0 +1,35 @@ +using System; +using System.IO; +using System.Net; + +namespace Examples.System.Net +{ + public class WebRequestGetExample + { + public static void Main() + { + // Create a request for the URL. + WebRequest request = WebRequest.Create( + "http://www.contoso.com/default.html"); + // If required by the server, set the credentials. + request.Credentials = CredentialCache.DefaultCredentials; + // Get the response. + WebResponse response = request.GetResponse(); + // Display the status. + Console.WriteLine(((HttpWebResponse)response).StatusDescription); + // Get the stream containing content returned by the server. + // The using block ensures the stream is automatically closed. + using (Stream dataStream = response.GetResponseStream()) + { + // Open the stream using a StreamReader for easy access. + StreamReader reader = new StreamReader(dataStream); + // Read the content. + string responseFromServer = reader.ReadToEnd(); + // Display the content. + Console.WriteLine(responseFromServer); + } + // Close the response. + response.Close(); + } + } +} \ No newline at end of file diff --git a/snippets/csharp/VS_Snippets_Network/SendDataUsingWebRequest/cs/WebRequestPostExample.cs b/snippets/csharp/VS_Snippets_Network/SendDataUsingWebRequest/cs/WebRequestPostExample.cs new file mode 100644 index 00000000000..21e25ed20aa --- /dev/null +++ b/snippets/csharp/VS_Snippets_Network/SendDataUsingWebRequest/cs/WebRequestPostExample.cs @@ -0,0 +1,48 @@ +using System; +using System.IO; +using System.Net; +using System.Text; + +namespace Examples.System.Net +{ + public class WebRequestPostExample + { + public static void Main() + { + // Create a request using a URL that can receive a post. + WebRequest request = WebRequest.Create("http://www.contoso.com/PostAccepter.aspx "); + // Set the Method property of the request to POST. + request.Method = "POST"; + // Create POST data and convert it to a byte array. + string postData = "This is a test that posts this string to a Web server."; + byte[] byteArray = Encoding.UTF8.GetBytes(postData); + // Set the ContentType property of the WebRequest. + request.ContentType = "application/x-www-form-urlencoded"; + // Set the ContentLength property of the WebRequest. + request.ContentLength = byteArray.Length; + // Get the request stream. + Stream dataStream = request.GetRequestStream(); + // Write the data to the request stream. + dataStream.Write(byteArray, 0, byteArray.Length); + // Close the Stream object. + dataStream.Close(); + // Get the response. + WebResponse response = request.GetResponse(); + // Display the status. + Console.WriteLine(((HttpWebResponse)response).StatusDescription); + // Get the stream containing content returned by the server. + // The using block ensures the stream is automatically closed. + using (dataStream = response.GetResponseStream()) + { + // Open the stream using a StreamReader for easy access. + StreamReader reader = new StreamReader(dataStream); + // Read the content. + string responseFromServer = reader.ReadToEnd(); + // Display the content. + Console.WriteLine(responseFromServer); + } + // Close the response. + response.Close(); + } + } +} \ No newline at end of file diff --git a/snippets/visualbasic/VS_Snippets_Network/RequestDataUsingWebRequest/vb/WebRequestGetExample.vb b/snippets/visualbasic/VS_Snippets_Network/RequestDataUsingWebRequest/vb/WebRequestGetExample.vb new file mode 100644 index 00000000000..221218973c2 --- /dev/null +++ b/snippets/visualbasic/VS_Snippets_Network/RequestDataUsingWebRequest/vb/WebRequestGetExample.vb @@ -0,0 +1,30 @@ +Imports System.IO +Imports System.Net + +Namespace Examples.System.Net + Public Class WebRequestGetExample + Public Shared Sub Main() + ' Create a request for the URL. + Dim request As WebRequest = + WebRequest.Create("http://www.contoso.com/default.html") + ' If required by the server, set the credentials. + request.Credentials = CredentialCache.DefaultCredentials + ' Get the response. + Dim response As WebResponse = request.GetResponse() + ' Display the status. + Console.WriteLine(CType(response, HttpWebResponse).StatusDescription) + ' Get the stream containing content returned by the server. + ' The using block ensures the stream is automatically closed. + Using dataStream As Stream = response.GetResponseStream() + ' Open the stream using a StreamReader for easy access. + Dim reader As New StreamReader(dataStream) + ' Read the content. + Dim responseFromServer As String = reader.ReadToEnd() + ' Display the content. + Console.WriteLine(responseFromServer) + End Using + ' Clean up the response. + response.Close() + End Sub + End Class +End Namespace \ No newline at end of file diff --git a/snippets/visualbasic/VS_Snippets_Network/SendDataUsingWebRequest/vb/WebRequestPostExample.vb b/snippets/visualbasic/VS_Snippets_Network/SendDataUsingWebRequest/vb/WebRequestPostExample.vb new file mode 100644 index 00000000000..6e4697eb09b --- /dev/null +++ b/snippets/visualbasic/VS_Snippets_Network/SendDataUsingWebRequest/vb/WebRequestPostExample.vb @@ -0,0 +1,43 @@ +Imports System.IO +Imports System.Net +Imports System.Text + +Namespace Examples.System.Net + Public Class WebRequestPostExample + Public Shared Sub Main() + ' Create a request using a URL that can receive a post. + Dim request As WebRequest = WebRequest.Create("http://www.contoso.com/PostAccepter.aspx ") + ' Set the Method property of the request to POST. + request.Method = "POST" + ' Create POST data and convert it to a byte array. + Dim postData As String = "This is a test that posts this string to a Web server." + Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData) + ' Set the ContentType property of the WebRequest. + request.ContentType = "application/x-www-form-urlencoded" + ' Set the ContentLength property of the WebRequest. + request.ContentLength = byteArray.Length + ' Get the request stream. + Dim dataStream As Stream = request.GetRequestStream() + ' Write the data to the request stream. + dataStream.Write(byteArray, 0, byteArray.Length) + ' Close the Stream object. + dataStream.Close() + ' Get the response. + Dim response As WebResponse = request.GetResponse() + ' Display the status. + Console.WriteLine(CType(response, HttpWebResponse).StatusDescription) + ' Get the stream containing content returned by the server. + ' The using block ensures the stream is automatically closed. + Using dataStream1 As Stream = response.GetResponseStream() + ' Open the stream using a StreamReader for easy access. + Dim reader As New StreamReader(dataStream1) + ' Read the content. + Dim responseFromServer As String = reader.ReadToEnd() + ' Display the content. + Console.WriteLine(responseFromServer) + End Using + ' Clean up the response. + response.Close() + End Sub + End Class +End Namespace \ No newline at end of file