PROWAREtech

articles » archived » asp-net » webrequest-and-webresponse-classes

ASP.NET: WebRequest and WebResponse Classes

Use the powerful WebRequest and WebResponse classes to download and upload data (.NET Framework).

WebRequest and WebResponse are very powerful. If just needing to download simple GET requests, use WebClient, but if needing to POST forms, add authentication, or use a proxy, then use WebRequest and WebResponse.

This example submits a basic POST request. As with WebClient, cookies can be sent.

<%@ Page Language="C#" %>
<script runat="server">
	void Page_Load(object sender, System.EventArgs e)
	{
		System.Net.WebRequest req = System.Net.WebRequest.Create("http://www.prowaretech.com/_echo.aspx"); // NOTE: this file is offline
		req.Method = System.Net.WebRequestMethods.Http.Post;
		req.ContentType = "application/x-www-form-urlencoded";
		req.Headers.Add("Cookie: username=mrsmith");
		byte[] body = System.Text.Encoding.ASCII.GetBytes("firstname=john&lastname=smith");
		req.ContentLength = body.Length;
		using (System.IO.Stream reqStream = req.GetRequestStream())
		{
			reqStream.Write(body, 0, body.Length);
			System.Net.WebResponse resp = req.GetResponse();
			using (System.IO.Stream respStream = resp.GetResponseStream())
			{
				using(System.IO.StreamReader reader = new System.IO.StreamReader(respStream))
				{
					Response.Write(reader.ReadToEnd());
				}
			}
		}
	}
</script>

This code might send a proper multipart/form-data POST request. It goes untested.

<%@ Page Language="C#" EnableViewState="false" ViewStateMode="Disabled" %>
<script runat="server">
	void AppendStringToBody(ref byte[] body, string toAppend)
	{
		byte[] append = System.Text.Encoding.ASCII.GetBytes(toAppend);
		byte[] temp = new byte[body.Length + append.Length];
		body.CopyTo(temp, 0);
		append.CopyTo(temp, body.Length);
		body = temp;
	}
	void Page_Load(object sender, System.EventArgs e)
	{
		string boundary = "---------------------------7e123d1c000ae";
		System.Net.WebRequest req = System.Net.WebRequest.Create("http://www.prowaretech.com/_echo.aspx"); // NOTE: this file is offline
		req.Method = "POST";
		req.ContentType = "multipart/form-data; boundary=" + boundary;
		byte[] body = { };
		AppendStringToBody(ref body, boundary + "\r\nContent-Disposition: form-data; name=\"textfile\"; filename=\"somefile.txt\"\r\nContent-Type: text/plain\r\n\r\n");
		// append large amount of text
		AppendStringToBody(ref body, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec egestas imperdiet purus, sit amet aliquet sapien gravida quis. Curabitur sed velit eu est finibus malesuada et eu nisl. Curabitur vel sapien et massa suscipit rhoncus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Quisque commodo, libero vitae tincidunt volutpat, felis elit lacinia nibh, id vehicula tellus erat ut ex. Vivamus vehicula arcu sit amet arcu tincidunt, nec ultricies arcu suscipit. Donec tempus metus nec faucibus gravida. Vivamus eu ligula feugiat, pharetra massa ac, efficitur tellus. Quisque faucibus nisi aliquet congue condimentum. Praesent tellus lectus, vestibulum sit amet lacus ac, sagittis rutrum velit. Morbi tempus accumsan cursus. Morbi egestas nisi quis neque lobortis interdum. Sed tristique augue nec dignissim condimentum.");
		AppendStringToBody(ref body, "\r\n" + boundary + "\r\nContent-Disposition: form-data; name=\"firstname\"\r\n\r\n"); // boundary and form field name
		AppendStringToBody(ref body, "john"); // firstname
		AppendStringToBody(ref body, "\r\n" + boundary + "\r\nContent-Disposition: form-data; name=\"lastname\"\r\n\r\n"); // boundary and form field name
		AppendStringToBody(ref body, "smith"); // lastname
		AppendStringToBody(ref body, "\r\n" + boundary + "--\r\n"); // ending boundary
		req.ContentLength = body.Length; // assign the length header
		using (System.IO.Stream reqStream = req.GetRequestStream())
		{
			reqStream.Write(body, 0, body.Length);
			System.Net.WebResponse resp = req.GetResponse();
			using (System.IO.Stream respStream = resp.GetResponseStream())
			{
				using(System.IO.StreamReader reader = new System.IO.StreamReader(respStream))
				{
					Response.Write(reader.ReadToEnd());
				}
			}
		}
	}
</script>

To provide authentication credentials with the request, create an instance of NetworkCredential.

<%@ Page Language="C#" ContentType="text/plain" %>
<script runat="server">
	void Page_Load(object sender, System.EventArgs e)
	{
		System.Net.WebRequest req = System.Net.WebRequest.Create("http://www.prowaretech.com/_echo.aspx"); // NOTE: this file is offline
		req.Credentials = new System.Net.NetworkCredential("username_here", "password_here");
		req.Method = System.Net.WebRequestMethods.Http.Get;
		System.Net.WebResponse resp = req.GetResponse();
		using (System.IO.Stream respStream = resp.GetResponseStream())
		{
			using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream))
			{
				Response.Write(reader.ReadToEnd());
			}
		}
	}
</script>

To use a proxy, create a WebProxy object and assign the Credentials property a NetworkCredential object. Then, create the WebRequest and assign the WebProxy object to its Proxy property.

<%@ Page Language="C#" ContentType="text/plain" %>
<script runat="server">
	void Page_Load(object sender, System.EventArgs e)
	{
		System.Net.WebProxy prox = new System.Net.WebProxy("192.168.1.254", true); // true to bypass the proxy for local addresses
		prox.Credentials = new System.Net.NetworkCredential("proxy_username_here", "proxy_password_here");
		System.Net.WebRequest req = System.Net.WebRequest.Create("http://www.prowaretech.com/_echo.aspx"); // NOTE: this file is offline
		req.Proxy = prox;
		req.Method = System.Net.WebRequestMethods.Http.Get;
		System.Net.WebResponse resp = req.GetResponse();
		using (System.IO.Stream respStream = resp.GetResponseStream())
		{
			using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream))
			{
				Response.Write(reader.ReadToEnd());
			}
		}
	}
</script>

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.
CLOSE