title: Exception:"The request was aborted: The connection was closed unexpectedly" When invoking two methods same object
body: <p>I am having a problem that many people have experienced but all of the solution that are available online are specific to their scenarios. I have tried the suggestions available but still no luck wha I have tried was:</p>
  
  <pre><code>1.) req.KeepAlive = false;
  2.) req.ProtocolVersion = HttpVersion.Version10; (this solved a different exception that I was getting)
  </code></pre>
  
  <p>I have two methods passing XML string to the server and getting responses back the two methods are: </p>
  
  <pre><code>public string userDeviceQuery(string userID)
      {
          string query = "xml=&lt;query&gt;";
          query += "&lt;appInfo&gt;";
          query += "&lt;appID&gt;" + appID + "&lt;/appID&gt;";
          query += "&lt;appCertificate&gt;" + appCertificate + "&lt;/appCertificate&gt;";
          query += "&lt;/appInfo&gt;";
          query += "&lt;userDevicesQuery&gt;";
          query += "&lt;userID&gt;";
          query += userID;
          query += "&lt;/userID&gt;";
          query += "&lt;/userDevicesQuery&gt;";
          query += "&lt;/query&gt;";
  
          using (Stream str = req.GetRequestStream())
          {
              str.Write(System.Text.Encoding.ASCII.GetBytes(query), 0, query.Length);
          }
  
          WebResponse res = req.GetResponse();
  
          string stringResponse;
          using (StreamReader reader = new StreamReader(res.GetResponseStream()))
          {
              stringResponse = reader.ReadToEnd();
          }
  
          string result = parseDeviceQueryRes(stringResponse);
          return result;
      }
  </code></pre>
  
  <p>AND</p>
  
  <pre><code>public void logoutOfEM(string deviceName)
      {
          string lgRequest = "xml=&lt;request&gt;";
                 lgRequest += "&lt;appInfo&gt;";
                 lgRequest += "&lt;appID&gt;" + appID + "&lt;/appID&gt;";
                 lgRequest += "&lt;appCertificate&gt;" + appCertificate + "&lt;/appCertificate&gt;";
                 lgRequest += "&lt;/appInfo&gt;";
                 lgRequest += "&lt;logout&gt;";
                 lgRequest += "&lt;deviceName&gt;";
                 lgRequest += deviceName;
                 lgRequest += "&lt;/deviceName&gt;";
                 lgRequest += "&lt;/logout&gt;";
                 lgRequest += "&lt;/request&gt;";
  
           using (Stream str = req.GetRequestStream())
          {
              str.Write(System.Text.Encoding.ASCII.GetBytes(lgRequest), 0, lgRequest.Length);
          }
  
          WebResponse res = req.GetResponse();
  
          using (StreamReader reader = new StreamReader(res.GetResponseStream()))
          {
              stringResponse = reader.ReadToEnd();
          }
          string stringResponse = reader.ReadToEnd();
      }
  </code></pre>
  
  <p>They are both part of a class called EMAPI. I can use both methods just fine separately but if try to use them back-to-back with the same object representing EMAPI I get the exception described above. I am not sure what is causing the connection to close, I would like to be able to keep it open and close it using a de-constructor if possible.</p>
  
  <p>For those who are curious this code deals with Cisco Extension Mobility API, first method queries the server to get a device you are logged in, second method logs teh user out of that device.</p>
  
  <p>Thanks for the help.</p>