Reputation: 3
I want to get the HTML code from http://www.w3schools.com/ Here is my code:
static void Main(string[] args)
{
TcpClient client = new TcpClient("www.w3schools.com", 80);
client.SendTimeout = 3000;
client.ReceiveTimeout = 3000;
StreamWriter writer = new StreamWriter(client.GetStream());
StreamReader reader = new StreamReader(client.GetStream());
writer.WriteLine("GET www.w3schools.com HTTP/1.1");
writer.WriteLine("Host: www.w3schools.com");
writer.WriteLine();
writer.Flush();
string response = reader.ReadToEnd();
Console.WriteLine("Got Response: {0}", response);
Console.ReadLine();
}
But I get the following:
Where I'm wrong?
Upvotes: 0
Views: 1881
Reputation: 1040
The below code will work even without TcpServer.
public static void getSavedHtmlCode()
{
string html = string.Empty;
try
{
var request = System.Net.HttpWebRequest.Create(string.Format("{0}", "https://www.w3schools.com/html/default.asp"));
request.Method = "GET";
var response = (HttpWebResponse)request.GetResponse();
//prepare as html
//html = new StreamReader(response.GetResponseStream()).ReadToEnd();
// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream();
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
//prepare as html
html = readStream.ReadToEnd();
Console.WriteLine("Response stream received.");
Console.WriteLine(html);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
Upvotes: 0
Reputation: 52280
The second element of the GET line should be the query path, not the domain name. This should work:
writer.WriteLine("GET / HTTP/1.1");
writer.WriteLine("Host: www.w3schools.com");
Upvotes: 1