title: use TcpClient to send request to server
body: <p>I am asking this question primarily because I do not have some ideas clear. I belive I understand how a webserver works but for some reason I am getting different results than expected.</p>
  
  <p><strong>So basically I want to replicate what I do with a real web browser with code.</strong></p>
  
  <p>I have a program called Fiddler that acts as a proxy in order to see all the requests and responses from the web server. </p>
  
  <p><strong>1. So when I open my broser and then goto <code>http://10.10.10.28/tfs:8080</code> this is what shows up:</strong></p>
  
  <p>-------- <img src="https://i.stack.imgur.com/wYa7f.png" alt="enter image description here"></p>
  
  <p>.    . . . and this is what fiddler records:</p>
  
  <p><img src="https://i.stack.imgur.com/hRUBz.png" alt="enter image description here"></p>
  
  <p>when I click cancel or attempt to log in other requests will be made and fiddler will record more data. I don't care about that right know I am just interested on simulating this first request.</p>
  
  <p>Anyways so fiddler tels us that the header was:</p>
  
  <pre><code>GET http://10.10.10.28/tfs:8080 HTTP/1.1
  Host: 10.10.10.28
  Connection: keep-alive
  Cache-Control: max-age=0
  User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.83 Safari/537.1
  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  Accept-Encoding: gzip,deflate,sdch
  Accept-Language: en-US,en;q=0.8
  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
  </code></pre>
  
  <p>and the response was:</p>
  
  <pre><code>HTTP/1.1 401 Unauthorized
  Server: Microsoft-IIS/7.5
  WWW-Authenticate: Negotiate
  WWW-Authenticate: NTLM
  X-Powered-By: ASP.NET
  MicrosoftSharePointTeamServices: 12.0.0.6421
  Date: Fri, 24 Aug 2012 14:36:22 GMT
  Content-Length: 0
  Proxy-Support: Session-Based-Authentication
  </code></pre>
  
  <p><strong>2. Finally getting to the fun part the code</strong> Now I will like to send the same header and expect to get the same response. <strong>For some reason I get a different response!</strong></p>
  
  <pre><code>public static void Main(string[] args)
  {
      // I save the header bytes recorded from fiddler on a file to make sure I am sending the exact same request
      byte[] header = System.IO.File.ReadAllBytes(@"C:\Users\Antonio\Desktop\header");
  
      // create the client
      TcpClient client = new TcpClient("10.10.10.28", 8080);
  
      // get the stream so that we can read and write to it
      var stream = client.GetStream();
  
      // now that we have the stream wait for the server to respond
      WaitForResponse(stream); // waits on a separate thread
  
      // send the request to the header
      stream.Write(header, 0, header.Length);
  
      // wait
      Console.Read();
  }
  
  public static void WaitForResponse(NetworkStream stream)
  {
      Task.Factory.StartNew(() =&gt; {
          byte[] buffer = new byte[16384];
          int responseLength = stream.Read(buffer, 0, buffer.Length);
          string resp = System.Text.UTF8Encoding.UTF8.GetString(buffer, 0, responseLength);
          resp = resp; // place breakpoint
      });
      System.Threading.Thread.Sleep(10); // make sure task starts
  }
  </code></pre>
  
  <p>here is the response that I get:
  <img src="https://i.stack.imgur.com/gOxvv.png" alt="enter image description here"></p>
  
  <p><strong>Why am I getting a different response?</strong> I believe web servers use a tcp connection to send pages to clients. why is this approach that I am taking not working? Also <strong>why is fiddler not recording anything when I send the request to the web-server from code?</strong> How does Google chrome connect to the web server? I would bet that the chrome browser is establishing also a tcp connection with the web server. </p>