net(客戶端)調用IIS(服務端)出現503后,就報操作超時錯誤
問題描述:
服務端環境:
IIS
客戶端環境:
windowsxp + iis + .net
調用時出現如下錯誤:
System.Net.WebException: 遠程服務器返回錯誤: (503) 服務器不可用。
在 System.Net.HttpWebRequest.GetResponse()
在 TestWebRequest.WebMessage.SendRequest(Byte[] data, String urlStr)
服務端環境:
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
;
修改后的方法為:
1
public byte[] SendRequest(byte[] data, string urlStr)2
{3
try4
{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
try20
{21
streamSend = myRequest.GetRequestStream();22
streamSend.Write(data, 0, data.Length);23
streamSend.Close();24
}25
catch (WebException wex)26
{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)35
{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
try46
{47
streamRequest = myRequest.GetResponse().GetResponseStream();48
}49
catch (Exception httpex)50
{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, 0, 256);59
while (count > 0)60
{61
bytes.writeByteArr(Bytes.byteSub(byteArr, 0, count));62
count = streamRequest.Read(byteArr, 0, 256);63
}64
streamRequest.Close();65
return bytes.getByte;66
}67
catch (Exception eee)68
{69
log.Debug("eee=" + eee.ToString() + eee.Source + eee.StackTrace);70
}71
return null;72
}
太晚了,睡覺。
