GET / HTTP/1.1
Accept: application/x-ms-application, */*
Accept-Language: en,fr-BE;q=0.5
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729
Accept-Encoding: gzip, deflate
Host: intranet.fabrikam.com
Connection: Keep-Alive
- Using System.Net.Sockets.Socket
- Using WebClient or WebRequest (method A)
- Using WebClient or WebRequest (method B)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
string strHttpRequest = String.Concat("GET / HTTP/1.1rn", "Accept: application/x-ms-application, */*rn", "Accept-Language: en,fr-BE;q=0.5rn", "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729rn", "Accept-Encoding: gzip, deflatern", "Host: intranet.fabrikam.comrn", "Connection: Keep-Alivernrn"); //Create a IPv4 TCP Socket Socket socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //Connect to the Front End A's IP address on HTTP Port (80) socketClient.Connect(IPAddress.Parse("10.0.0.1"), 80); //Send the Http Request socketClient.Send(Encoding.Default.GetBytes(strHttpRequest)); StringBuilder sbHttpResponse = new StringBuilder(); byte[] bReceiveBuffer = new byte[4096]; int iReceivedBytes = 0; //Process the Http Response while ((iReceivedBytes = socketClient.Receive(bReceiveBuffer)) > 0) sbHttpResponse.Append(Encoding.Default.GetString(bReceiveBuffer, 0, iReceivedBytes)); //Output the Http Response Console.WriteLine(sbHttpResponse.ToString()); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//Create a HttpWebRequest to Front End A's IP Address HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://10.0.0.1"); //Modify Host header to reach intranet.fabrikam.com myHttpWebRequest.Headers[HttpRequestHeader.Host] = "intranet.fabrikam.com"; //Process Response HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); Stream streamResponse = myHttpWebResponse.GetResponseStream(); StreamReader streamReader = new StreamReader(streamResponse); string strHttpResponse = streamReader.ReadToEnd(); //Output Response Console.WriteLine(strHttpResponse); streamResponse.Close(); streamReader.Close(); myHttpWebResponse.Close(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//Create a WebClient WebClient webClient = new WebClient(); //You can eventulay set a Username / Password as NetworkCredentials webClient.Credentials = new NetworkCredential("username", "password"); //The Front End A's IP Address to connect to webClient.Proxy = new WebProxy("10.0.0.1"); //Will set the correct Host Header Stream streamResponse = webClient.OpenRead("http://intranet.fabrikam.com"); //Process the HttpResponse StreamReader streamReader = new StreamReader(streamResponse); string strHttpResponse = streamReader.ReadToEnd(); //Output the HttpResponse Console.WriteLine(strHttpResponse); streamResponse.Close(); webClient.Dispose(); |