title: HttpWebRequest fails to connect everytime unless I restart the computer
body: <p>I have a WPF C# application that needs to connect to a remote SAP server. I'm running the application on two computers (same network), and I know that the server is reachable.</p>
  
  <p>At first, computer A can connect fine using the web request (after making several tries, they all work fine). Randomly (it seems; I can't duplicate), all of the web requests will fail with the message "Unable to connect to the remote server" (type System.Net.WebException). Navigating to the URL in a browser loads fine; only the application cannot connect. The method that throws the exception is HttpWebRequest.GetRequestStream.</p>
  
  <p>Even after restarting the application several times, all HttpWebRequests still fail to connect. We have multiple SAP servers, attempting to connect to any of them gives the same results. Meanwhile, the application on computer B (my development machine) can still connect to any of the servers with no problems. Only after restarting Computer A entirely, is it able to connect again.</p>
  
  <p>This has happened on two different computers so far. Both times, the only way to resolve it was by restarting the computer.</p>
  
  <p>Here's some code:</p>
  
  <pre><code>HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  request.Method = "POST";
  request.ContentType = "text/xml; charset=utf-8";
  request.ContentLength = contentLength;
  request.Headers.Add("Authorization", GetAuthHeader());
  request.Timeout = GetTimeout();
  
  Stream post = request.GetRequestStream();//this line throws the exception
  //...write to stream...
  post.Close();
  </code></pre>
  
  <p>Edit: Overlooked the response part:</p>
  
  <pre><code>HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  Stream responsedata = response.GetResponseStream();
  //...read all response data...
  if (responsedata != null)
      responsedata.Close();
  </code></pre>
  
  <p>Edit2:
  So I commented out all of the calls to Close, and made a couple dozen requests. I also removed the code that gets the response and made several more requests. During these tests I received no exceptions and everything worked properly.</p>
  
  <p>These results aside, I can understand something like this could break my program, but I don't understand why it would still be broken after restarting the program or trying to contact a different remote server.
  It seams like something is getting messed up in the .NET framework that doesn't resolve itself unless I restart the whole machine.</p>