最近使用多线程同时调用一个Web Service,总是有很多调用报超时,代码类似于下面的代码(为了简化,我把调用Web Service改为使用HttpWebRequest访问一个网址,效果时一样的):
我循环调用100次,但是总是有几十次调用报超时,具体如下:
System.Net.WebException: 操作已超时。
at System.Net.HttpWebRequest.GetResponse()
at HttpRequestTest.HttpRequestTest.MakeWebRequest(Object obj) in g:\my documents\visual studio projects\httprequesttest\httprequesttest.cs:line 35
真的不知道为什么,难道Web Service就不能在多线程的环境下调用!
因为目前找不到原因,暂时放在首页,想向各位请教,明天就撤调,请手下留情,谢谢!
1
using
System;
2
using
System.Diagnostics;
3
using
System.Net;
4
using
System.Threading;
5![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/None.gif)
6
namespace
HttpRequestTest
7![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
8![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
9
/// Class1 的摘要说明。
10
/// </summary>
11
class HttpRequestTest
12![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
//[STAThread]
15
static void Main(string[] args)
16![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
17
for(int i = 0; i < 100; i++ )
18![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19
ThreadPool.QueueUserWorkItem(new WaitCallback(MakeWebRequest),"http://www.163.com");
20
}
21
Console.ReadLine();
22
}
23![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
private static void MakeWebRequest (object obj)
25![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
26
string url = obj as string;
27
HttpWebResponse res = null;
28
try
29![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
30
HttpWebRequest req = (HttpWebRequest)WebRequest.Create (url);
31
req.Timeout = 15000;
32![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
Console.WriteLine ("\nConnecting to " + url + " ![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
");
34![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
res = (HttpWebResponse)req.GetResponse ();
36![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
Console.WriteLine("[" + AppDomain.GetCurrentThreadId() + "] ContentLength:" + res.ContentLength);
38
Console.WriteLine ("Connected.\n");
39![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
}
42
catch (Exception e)
43![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
44
Console.WriteLine ("Source : " + e.Source);
45
Console.WriteLine ("Message : " + e.Message);
46
Console.WriteLine(e.ToString());
47
Debug.WriteLine(e.ToString());
48
//Console.WriteLine("StackTrace :" + e.StackTrace);
49
}
50
finally
51![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
52
if (res != null)
53![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
54
res.Close ();
55
}
56
}
57
}
58
}
59
}
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
8
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
9
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
11
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
13
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
17
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
19
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
21
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
23
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
26
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
27
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
30
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
34
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
42
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
44
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
50
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
52
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
54
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
56
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
57
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
58
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
59
![](https://walkinhill.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)