PROWAREtech

articles » archived » asp-net » webclient-class

ASP.NET: Connect with WebClient

Use the WebClient class to download or upload data to a URL / URI (.NET Framework).

WebClient is a very basic, easy way to request data from a particular URI. The easiest method to use is DownloadFile() which takes a URI and then the path to save it to.

<%@ Page Language="C#" %>
<script runat="server">
	void Page_Load(object sender, System.EventArgs e)
	{
		using (System.Net.WebClient wc = new System.Net.WebClient())
		{
			wc.DownloadFile("http://www.prowaretech.com/", Server.MapPath("/") + "homepage.html");
			Response.Redirect("homepage.html");
		}
	}
</script>
<html>
	<head><title></title></head>
	<body>
	</body>
</html>

To process the data within the application, use the method OpenRead() to get a System.IO.Stream object.

<%@ Page Language="C#" %>
<script runat="server">
	void Page_Load(object sender, System.EventArgs e)
	{
		using (System.Net.WebClient wc = new System.Net.WebClient())
		{
			using (System.IO.Stream stream = wc.OpenRead("http://www.prowaretech.com/"))
			{
				using (System.IO.StreamReader reader = new System.IO.StreamReader(stream))
				{
					Response.Write(reader.ReadToEnd());
				}
			}
		}
	}
</script>

To send a Cookie to the server, use the Headers.Add() method.

<%@ Page Language="C#" %>
<script runat="server">
	void Page_Load(object sender, System.EventArgs e)
	{
		using (System.Net.WebClient wc = new System.Net.WebClient())
		{
			wc.Headers.Add("Cookie: username=mrsmith");
			using (System.IO.Stream stream = wc.OpenRead("http://www.prowaretech.com/"))
			{
				using (System.IO.StreamReader reader = new System.IO.StreamReader(stream))
				{
					Response.Write(reader.ReadToEnd());
				}
			}
		}
	}
</script>

To send data like a file to a server, use the UploadFile() method.

<%@ Page Language="C#" %>
<script runat="server">
	void Page_Load(object sender, System.EventArgs e)
	{
		using (System.Net.WebClient wc = new System.Net.WebClient())
		{
			wc.UploadFile("http://www.domain.com/NewFileName.jpg", @"C:\Images\File.jpg");
		}
	}
</script>

The UploadData() method allows uploading binary data.

<%@ Page Language="C#" %>
<script runat="server">
	void Page_Load(object sender, System.EventArgs e)
	{
		using (System.Net.WebClient wc = new System.Net.WebClient())
		{
			byte[] bytes = { 1, 9, 2, 5, 3, 4, 7, 8, 9, 5, 3, 1, 7, 9, 0 };
			wc.UploadData("http://www.domain.com/BinaryData.bin", bytes);
		}
	}
</script>

To send data (like a POST) to a URI and retrieve a response, use the more complicated but also more comprehensive WebRequest & WebResponse objects.

There is another method not covered here: OpenWrite(), which allows for HTTP PUT operations. It does not get the response and as such is not very useful. There is also a DownloadData() method used for downloading into a byte array.


PROWAREtech

Hello there! How can I help you today?
Ask any question

PROWAREtech

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
ACCEPT REJECT