C# post数据时 出现如下错误: System.Net.WebException: 操作超时

net(客户端)调用IIS(服务端)出现503后,就报操作超时错误

问题描述:
服务端环境:
IIS

客户端环境:
windowsxp + iis + .net


调用时出现如下错误:
System.Net.WebException: 远程服务器返回错误: (503) 服务器不可用。
   在 System.Net.HttpWebRequest.GetResponse()
   在 TestWebRequest.WebMessage.SendRequest(Byte[] data, String urlStr)

接着就出现如下错误:

System.Net.WebException: 操作超时
   在 System.Net.HttpWebRequest.GetRequestStream()

最后一直是这个错误

System.Net.WebException: 操作超时
   在 System.Net.HttpWebRequest.GetRequestStream()

 当服务器恢复正常时,访问已经是200时,这个线程还是返回操作超时,经过N多测试,最后如下一行完美解决:

myRequest.ServicePoint.Expect100Continue  =   false ;

 修改后的方法为:

ContractedBlock.gif ExpandedBlockStart.gif Code
 1 public byte[] SendRequest(byte[] data, string urlStr)
 2ExpandedBlockStart.gifContractedBlock.gif        {
 3            try
 4ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 5                Stream streamSend = null;
 6                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(urlStr);
 7                myRequest.Method = "POST";
 8                myRequest.ContentType = "text/xml";
 9                myRequest.Accept = "*/*";
10                myRequest.Timeout = 2000;
11                myRequest.UserAgent = "Mozilla-Firefox-Spider(Wenanry)";
12                myRequest.ContentLength = data.Length;
13                //这个在Post的时候,一定要加上,如果服务器返回错误,他还会继续再去请求,不会使用之前的错误数据,做返回数据
14                myRequest.ServicePoint.Expect100Continue = false;
15
16                HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
17                myRequest.CachePolicy = noCachePolicy;
18
19                try
20ExpandedSubBlockStart.gifContractedSubBlock.gif                {
21                    streamSend = myRequest.GetRequestStream();
22                    streamSend.Write(data, 0, data.Length);
23                    streamSend.Close();
24                }

25                catch (WebException wex)
26ExpandedSubBlockStart.gifContractedSubBlock.gif                {
27                    log.Debug("WebException=" + wex.ToString() + ",wex.Status=" + wex.Status);
28                    if (streamSend != null)
29                        streamSend.Close();
30                    streamSend = null;
31                    myRequest = null;
32                    return null;
33                }

34                catch (Exception ex)
35ExpandedSubBlockStart.gifContractedSubBlock.gif                {
36                    log.Debug("GetRequestStream=" + ex.ToString());
37                    if (streamSend != null)
38                        streamSend.Close();
39                    myRequest = null;
40                    return null;
41                }

42
43                byte[] byteArr = new byte[256];
44                Stream streamRequest = null;
45                try
46ExpandedSubBlockStart.gifContractedSubBlock.gif                {
47                    streamRequest = myRequest.GetResponse().GetResponseStream();
48                }

49                catch (Exception httpex)
50ExpandedSubBlockStart.gifContractedSubBlock.gif                {
51                    log.Debug("SendRequest=" + httpex.ToString());
52                    if (streamRequest != null)
53                        streamRequest.Close();
54                    myRequest = null;
55                    return null;
56                }

57                Bytes bytes = new Bytes();
58                int count = streamRequest.Read(byteArr, 0256);
59                while (count > 0)
60ExpandedSubBlockStart.gifContractedSubBlock.gif                {
61                    bytes.writeByteArr(Bytes.byteSub(byteArr, 0, count));
62                    count = streamRequest.Read(byteArr, 0256);
63                }

64                streamRequest.Close();
65                return bytes.getByte;
66            }

67            catch (Exception eee)
68ExpandedSubBlockStart.gifContractedSubBlock.gif            {
69                log.Debug("eee=" + eee.ToString() + eee.Source + eee.StackTrace);
70            }

71            return null;
72        }

 

太晚了,睡觉。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值