I am facing a web requests responses mixed up.
I am developping a C# project on mobile device (Pocket PC 2003).
Here is the context
My software has a thread running every 5 seconds to ping a server, send its status and retrieve some server information : function ping()
Depending on the user interface, I contact the same server to declare a new user action : function sendNewEvent()
These two actions are using the following function request()
which contacts the server, wait its response and return the server response.
It is working, but in some cases, the server response is mixed up : the ping()
function receives the response of the sendNewEvent()
function, even if the functions are not played at the same time.
I don't understand how and why it is possible. Is there someone who experiment the same problem?
Is there something (HttpWebResponse, WebRequest, ...) in the request()
function which is not thread safe?
Update
I am sure that the server sends the right data by request. All requests and server answers are logged into a DB table.
I tried this solution, but it is not working in my case : HttpWebResponse get mixed up when used inside multiple threads
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;
}
}