WebRequest is a request to send messages to a URI to send messages, URI as a parameter is passed to Create () method. And we take WebResponse class as data obtained from the server. The method WebRequest.GetResponse () will indeed send the request to the Web server, create a Response object, and then check the returned data. Like the WebClient object, you can get a stream of data, however, the data flow is obtained by WebResponse.GetResponseStream () method.
Here is a smple to show you how to get data from URI and save as a html file:
Get Data From Uri
/**//// <summary>
/// Get data from url and save as html file.
/// </summary>
/// <param name="uri">The uri where you want to get data from</param>
/// <param name="htmlFile">File used to save data</param>
public static void GetDataFromUri(string uri, string htmlFile)
{
//Makes a request to specified URI.
System.Net.WebRequest request = WebRequest.Create(uri);
System.Net.WebResponse webResponse = request.GetResponse();
//Initialize a new instance of the System.IO.StreamReader class for the web response stream,
//with the specified character encoding, and get all data.
StreamReader sr = new StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.Default);
string dataBuffer = sr.ReadToEnd();
FileStream output = new FileStream(htmlFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
byte[] recvBuffer = System.Text.Encoding.Default.GetBytes(dataBuffer);
BinaryWriter bw = new BinaryWriter(output);
bw.Write(recvBuffer);
//If we use this method to write data, some characters will be garbled.
//StreamWriter sw = new StreamWriter(output);
//sw.Write(dataBuffer);
}
Note this: If we use StreamWriter to write directly, we will get some garbled characters in the html file. Of course that's the encoding problem.
Go to my home page for more posts