title: WebRequest responses mixed up in multithreaded environment
body: <p>I am facing a web requests responses mixed up.</p>
  
  <p>I am developping a C# project on mobile device (Pocket PC 2003). </p>
  
  <p><strong>Here is the context</strong></p>
  
  <p>My software has a thread running every 5 seconds to ping a server, send its status and retrieve some server information : function <code>ping()</code></p>
  
  <p>Depending on the user interface, I contact the same server to declare a new user action : function <code>sendNewEvent()</code></p>
  
  <p>These two actions are using the following function <code>request()</code> which contacts the server, wait its response and return the server response.</p>
  
  <p>It is working, but in some cases, the server response is mixed up : the <code>ping()</code> function  receives the response of the <code>sendNewEvent()</code> function, even if the functions are not played at the same time.</p>
  
  <p>I don't understand how and why it is possible. Is there someone who experiment the same problem? </p>
  
  <p>Is there something (HttpWebResponse, WebRequest, ...) in the <code>request()</code> function which is not thread safe?</p>
  
  <p><strong>Update</strong></p>
  
  <p>I am sure that the server sends the right data by request. All requests and server answers are logged into a DB table.</p>
  
  <p>I tried this solution, but it is not working in my case : <a href="https://stackoverflow.com/questions/3045449/httpwebresponse-get-mixed-up-when-used-inside-multiple-threads">HttpWebResponse get mixed up when used inside multiple threads</a></p>
  
  <pre><code>    public string request(string xUrl)
      {
          try
          {
              // authentication
              CredentialCache cc = new CredentialCache();
              cc.Add(new Uri(xUrl), "Basic", new NetworkCredential(PropertiesFile.getWSUser(), PropertiesFile.getWSPwd()));                
  
              // Create a request for the URL.        
              WebRequest request = WebRequest.Create(xUrl);
              request.Timeout = 5000;
              //request.KeepAlive = true;
              request.Credentials = cc;
              // Get the response.
              HttpWebResponse response = (HttpWebResponse)request.GetResponse();
              // Get the stream containing content returned by the server.
              Stream dataStream = response.GetResponseStream();
              // Open the stream using a StreamReader for easy access.
              StreamReader reader = new StreamReader(dataStream);
              // Read the content.
              string responseFromServer = reader.ReadToEnd();
              // Cleanup the streams and the response.
              reader.Close();
              dataStream.Close();
              response.Close();
  
              return responseFromServer;
          }
          catch(Exception e)
          {
              if (PropertiesFile.getShowDebugMessageBox())
                  MessageBox.Show("WebCalls request exception : " + e.Message);
  
              return null;
          }
      }
  </code></pre>