English 中文(简体)
HttpWebResponse在多个深层使用时混合使用
原标题:HttpWebResponse get mixed up when used inside multiple threads

在我的申请书中,我有几条线人,他们将从网络服务处获得数据。 基本上,我刚刚开放了URL,并获得XML产出。 我有几条直线人,他们不断这样做,但也有不同的URL。 有时结果好坏参半。 XML输出的表面上属于深层的URL,而属于另一个深层的URL。

在每一个路面,我都创立了“GetWebPage”类的例子,并将“Get”方法从这个角度讲。 这种方法非常简单,主要基于MSDN文件中的代码。 (见下文)。 我删除了我的错误处理!

    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.

EDIT

增 编

System.Net.ServicePointManager.DefaultConnectionLimit = 25;

而现在,这部建筑工程在相当一段时间内毫无错误。

问题回答

你的方法完全是read。

你在称为这一方法的法典中可能有一个问题。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签