using System.Net.Sockets;
public static void Main()
string url = "http://api.openweathermap.org/data/2.5/weather?q=Hanoi&mode=xml&appid=78dff84492be32f8b4f77692904607a1";
Console.WriteLine(CallViaWebClient(url));
Console.WriteLine(CallViaTCPClient(url));
public static string CallViaWebClient(string url)
WebClient client = new WebClient();
client.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
Stream data = client.OpenRead(url);
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
public static string CallViaTCPClient(string url)
Uri myUri = new Uri(url);
string domain = myUri.Host;
string geturl = myUri.PathAndQuery;
hostEntry = Dns.GetHostEntry(domain);
if (hostEntry.AddressList.Length > 0)
var ip = hostEntry.AddressList[0];
TcpClient tcpclient = new TcpClient(myUri.Host, 80);
StreamReader sr = new StreamReader(tcpclient.GetStream());
StreamWriter sw = new StreamWriter(tcpclient.GetStream());
data = "GET " + geturl + " HTTP/1.1";
data = "Host: " + domain;