using System.ComponentModel;
using System.IO;
using System.Net;
class MainClass
{
static void Main()
{
WebRequest request = WebRequest.Create("http://java2s.com");
using (WebResponse response = request.GetResponse())
using (Stream responseStream = response.GetResponseStream())
using (FileStream output = File.Create("response.dat"))
{
Copy(responseStream, output);
}
}
const int BufferSize = 8192;
public static void Copy(Stream input,Stream output)
{
byte[] buffer = new byte[BufferSize];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, read);
}
}
public static byte[] ReadFully(Stream input)
{
using (MemoryStream tempStream = new MemoryStream())
{
Copy(input, tempStream);
if (tempStream.Length == tempStream.GetBuffer().Length)
{
return tempStream.GetBuffer();
}
return tempStream.ToArray();
}
}
}
|