WebRequest Class
Assembly: System (in system.dll)
WebRequest is the abstract base class for the .NET Framework's request/response model for accessing data from the Internet. An application that uses the request/response model can request data from the Internet in a protocol-agnostic manner, in which the application works with instances of the WebRequest class while protocol-specific descendant classes carry out the details of the request.
Requests are sent from an application to a particular URI, such as a Web page on a server. The URI determines the proper descendant class to create from a list of WebRequest descendants registered for the application. WebRequest descendants are typically registered to handle a specific protocol, such as HTTP or FTP, but can be registered to handle a request to a specific server or path on a server.
The WebRequest class throws a WebException when errors occur while accessing an Internet resource. The WebException.Status property is one of the WebExceptionStatus values that indicates the source of the error. When WebException.Status is WebExceptionStatus.ProtocolError, the Response property contains the WebResponse received from the Internet resource.
Because the WebRequest class is an abstract class, the actual behavior of WebRequest instances at run time is determined by the descendant class returned by System.Net.WebRequest.Create method. For more information about default values and exceptions, see the documentation for the descendant classes, such as HttpWebRequest and FileWebRequest.
Note |
|---|
| Use the Create method to initialize new WebRequest instances. Do not use the WebRequest constructor. |
Note |
|---|
| If the application that creates the WebRequest object runs with the credentials of a Normal user, the application will not be able to access certificates installed in the local machine store unless permission has been explicitly given to the user to do so. |
The following example shows how to create a WebRequest instance and return the response.
using System; using System.IO; using System.Net; using System.Text; 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. HttpWebResponse response = (HttpWebResponse)request.GetResponse (); // Display the status. Console.WriteLine (response.StatusDescription); // Get the stream containing content returned by the server. 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); // Cleanup the streams and the response. reader.Close (); dataStream.Close (); response.Close (); } } }
- WebPermission To access the requested URI or any URI that the request is redirected to. Associated enumeration: Connect.
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
We can use WebRequest perform a post.
Just need code in this way:
C#
string urlPost = "http://adventureworks.com/api/post.xml";
string postData = "status=something strange";
UTF8Encoding encoding = new UTF8Encoding();
byte[] byteData = encoding.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlPost);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteData.Length;
request.Credentials = new NetworkCredential("xx", "xx");
Stream requestStream = request.GetRequestStream();
requestStream.Write(byteData, 0, byteData.Length);
requestStream.Close();
Maybe you think the request is working.
No, until you chek the response, the request is not performed.
C#
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string responseStatus = response.StatusDescription;
- 11/15/2007
- shiny zhu
If File.Exists(destPath) Then
Try
imgRequest = WebRequest.Create(imgPath.TrimStart)
imgResp = imgRequest.GetResponse
lastMod = DateTime.Parse(imgResp.Headers("Last-Modified"))
imgInfo = New FileInfo(destPath)
If DateTime.Compare(imgInfo.CreationTime, lastMod) > 0 Then
Console.WriteLine("SKIP - " & destPath)
Continue For
End If
Catch ex As Exception
Console.WriteLine("ERROR - " & imgPath)
Finally
If imgResp IsNot Nothing Then
imgResp.Close()
End If
End TryEnd If
Note this code does not include variables declaration or the For cycle that scan all the files. Also it refer to images, but of course it can be used with any kind of file.
- 9/2/2007
- LucaMauri
The following sample shows how to use the WebRequest class to screen scrape the contents of a Web page and use a regular expression to list the urls of the images on the page.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
class Program
{
static void Main(string[] args)
{
//read images from a url and display them in the console window
List<string> imageUrls = GetAllImagesFromUrl("http://www.msn.com");
foreach (string img in imageUrls)
{
Console.WriteLine(img);
}
Console.Read();
}
/// <summary>
/// Given a web page url, it will retrieve the Html from that page and parse the image tags in that page
/// </summary>
/// <param name="url">The Web page url in this format "http;//www.msn.com"</param>
/// <returns>Returns a list of image urls as strings based on the url of a Web page</returns>
public static List<string> GetAllImagesFromUrl(string url)
{
List<string> urlList = new List<string>();
string rawHtml = String.Empty;
//read the contents of the web page into a string
using (StreamReader sr = new StreamReader(new WebClient().OpenRead(url)))
{
rawHtml = sr.ReadToEnd();
}
//regular expression to part out <img> tags from the html
string regExPattern = @"< \s* img [^\>]* src \s* = \s* [\""\']? ( [^\""\'\s>]* )";
Regex r = new Regex(regExPattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
MatchCollection matches = r.Matches(rawHtml);
foreach (Match m in matches)
{
urlList.Add(m.Groups[1].Value);
}
return urlList;
}
}
This sample is adopted from a Google images sample I wrote that shows how to asynchronously do WebRequests to screen scrape images from images.google.com. More information and full source code at: http://blogs.msdn.com/danielfe/archive/2004/07/26/197811.aspx
- 6/5/2006
- Dan Fernandez - MSFT
