HttpWebResponse获得多线程内使用时混合起来 [英] HttpWebResponse get mixed up when used inside multiple threads

查看:224
本文介绍了HttpWebResponse获得多线程内使用时混合起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我有几个线程谁将会从Web服务获取数据。基本上我只是打开一个URL,并得到一个XML输出。我有几个线程谁做这个不断,但具有不同的URL。有时结果混淆。 XML输出不属于一个线程,但到另一个线程的URL网址。

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.

在每个线程创建类GetWebPage的一个实例,并调用方法从这个实例中获取。该方法非常简单,主要依据是从MSDN文档的代码。 (见下文。我删除了我在这里的错误处理!)

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;



正如你可以看到我没有公共属性或任何其他共享资源。至少我没有看到任何。
中的网址是我在互联网电话服务和缓存是来自服务器的XML输出。
就像我说我有几个线程的这一类/方法的多个实例(10至12),有时缓存不属于该在网址同样的线程,但另一个线程。

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.

修改

我添加

System.Net.ServicePointManager.DefaultConnectionLimit = 25;

和现在它PROGRAMM没有错误的作品相当长的一段时间。

and right now it programm works without error for quite some time.

推荐答案

您的方法完全是线程安全的。

Your method is entirely thread-safe.

您可能有一个问题,在代码中调用方法。

You probably have a problem in the code calling the method.

这篇关于HttpWebResponse获得多线程内使用时混合起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆