In my Application I have a few threads who will get data from a web service. Basically I just open an URL and get an XML output. I have a few threads who do this continuously but with different URLs. Sometimes the results are mixed up. The XML output doesn't belong to the URL of a thread but to the URL of another thread.
In each thread I create an instance of the class GetWebPage and call the method Get from this instance. The method is very simple and based mostly on code from the MSDN documentation. (See below. I removed my error handling here!)
public string Get(string userAgent, string url, string user, string pass, int timeout, int readwriteTimeout, WebHeaderCollection whc)
{
string buffer = string.Empty;
HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(url);
if (!string.IsNullOrEmpty(userAgent))
myWebRequest.UserAgent = userAgent;
myWebRequest.Timeout = timeout;
myWebRequest.ReadWriteTimeout = readwriteTimeout;
myWebRequest.Credentials = new NetworkCredential(user, pass);
string[] headers = whc.AllKeys;
foreach (string s in headers)
{
myWebRequest.Headers.Add(s, whc.Get(s));
}
using (HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse())
{
using (Stream ReceiveStream = myWebResponse.GetResponseStream())
{
Encoding encode = Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(ReceiveStream, encode);
// Read 1024 characters at a time.
Char[] read = new Char[1024];
int count = readStream.Read(read, 0, 1024);
int break_counter = 0;
while (count > 0 && break_counter < 10000)
{
String str = new String(read, 0, count);
buffer += str;
count = readStream.Read(read, 0, 1024);
break_counter++;
}
}
}
return buffer;
As you can see I have no public properties or any other shared resources. At least I don't see any.
The url is the service I call in the internet and buffer is the XML Output from the server.
Like I said I have multiple instances of this class/method in a few threads (10 to 12) and sometimes buffer does not belong the the url of the same thread but another thread.