Download image from a website
using System;
using System.Net;
using System.IO;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
public class MainClass {
[STAThread]
public static void Main(string[] args)
{
string picUri = "http://www.java.ru/style/logo.PNG";
// Create the requests.
WebRequest requestPic = WebRequest.Create(picUri);
WebResponse responsePic = requestPic.GetResponse();
Image webImage = Image.FromStream(responsePic.GetResponseStream());
}
}
Reading Web Page As HTML
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class Client
{
static public void Main(string[] Args)
{
HttpWebRequest webRequest =(HttpWebRequest)WebRequest.Create("http://www.google.ru/");
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
StreamReader streamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.ASCII);
string outputString = streamReader.ReadToEnd();
Console.WriteLine(outputString);
streamReader.Close();
}
}
Save what you read from WebRequest to a file
using System.ruponentModel;
using System.IO;
using System.Net;
class MainClass
{
static void Main()
{
WebRequest request = WebRequest.Create("http://nfex.ru");
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();
}
}
}
Use WebRequest
using System;
using System.Net;
using System.IO;
class MainClass
{
[STAThread]
static void Main(string[] args)
{
WebRequest MyRequest = WebRequest.Create("http://www.nfex.ru");
WebResponse MyResponse = MyRequest.GetResponse();
Stream MyStream = MyResponse.GetResponseStream();
StreamReader MyReader = new StreamReader(MyStream);
string MyWebLine;
while ((MyWebLine = MyReader.ReadLine()) != null)
{
Console.WriteLine(MyWebLine);
}
MyStream.Close();
}
}
Use WebRequest and WebResponse to read a web page
using System;
using System.Net;
using System.IO;
public class MainClass {
[STAThread]
public static void Main(string[] args)
{
string htmlUri = "http://www.nfex.ru";
WebRequest requestHtml = WebRequest.Create(htmlUri);
WebResponse responseHtml = requestHtml.GetResponse();
// Read the text from the response stream.
using (StreamReader r = new StreamReader(responseHtml.GetResponseStream()))
{
Console.WriteLine( r.ReadToEnd());
}
}
}
<HTML>
<HEAD>
Java examples (example source code) Organized by topic </title>
...
...
Use WebRequest to create a StreamReader
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
class MainClass
{
static void Main(string[] args)
{
StreamReader reader = new StreamReader(WebRequest.Create("http://www.nfex.ru").GetResponse().GetResponseStream());
string line;
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
<HTML>
<HEAD>
Java examples (example source code) Organized by topic </title>
...