Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
NetworkStream example
string host = "";
string page = "/search?q=networkstream";
// This is the simplest way of getting a NetworkStream back. It can also be
// done at a lower level using your own Socket class and the options you
// require.
TcpClient tcp = new TcpClient();
tcp.Connect(host, 80);
NetworkStream stream = tcp.GetStream();
// Send a HTTP request
byte[] data = Encoding.Unicode.GetBytes(string.Format("GET {0} HTTP/1.0{1}{1}", page, "\r\n"));
stream.Write(data, 0, data.Length);
// Get the returned data in 1k chunks.
byte[] buffer = new byte[1024];
int i = stream.Read(buffer, 0, 1024);
StringBuilder builder = new StringBuilder();
while (i > 0)
i = stream.Read(buffer, 0, 1024);
// Write out to a file. NB the server header will be contained in this file too.
using (StreamWriter writer = new StreamWriter(@"c:\out.html"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.