在我的申请书中,我有几条线人,他们将从网络服务处获得数据。 基本上,我刚刚开放了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;
而现在,这部建筑工程在相当一段时间内毫无错误。