2012-06-01 41 views
0

我正在使用如下所述的app_offline.htm文件:http://weblogs.asp.net/scottgu/archive/2005/10/06/426755.aspx可使旧的asmx Web服务脱机。app_offline.htm文件正在使web服务离线 - 可能读取文件的内容?

一切工作正常,和客户端得到一个HTTP 503异常,如:

Exception : System.Net.WebException 
The request failed with HTTP status 503: Service Unavailable. 
Source : System.Web.Services 
Stack trace : 
    at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall) 
    at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters) 

我的问题:是否有可能为客户端应用程序读取它会一直在app_offline.htm文件的内容回 ?该文件中的基本HTML具有有用的文本,如:“该应用程序目前正在维护中”。我可以看到使用Fiddler在响应中返回此文件的内容。

能够解析此html响应以向用户提供更多信息将是有用的。 (即,可以区分由于系统维护造成的503错误和由于系统过载等造成的503错误)。

编辑:BluesRockAddict的响应听起来很不错,但这个流目前似乎无法使用。例如:

  // wex is the caught System.Net.WebException 
      System.Net.WebResponse resp = wex.Response; 


      byte[] buff = new byte[512]; 
      Stream st = resp.GetResponseStream(); 

      int count = st.Read(buff, 0, 512); 

上面,它试图读取流最后一行给出:

Exception : System.ObjectDisposedException 
Cannot access a closed Stream. 
Source : mscorlib 
Stack trace : 
    at System.IO.__Error.StreamIsClosed() 
    at System.IO.MemoryStream.Read(Byte[] buffer, Int32 offset, Int32 count) 

回答

2

归功于BluesRockAddict,增加他的回答是,这是你如何阅读html页面的内容。

catch (WebException ex) 
{ 
    if (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.ServiceUnavailable) 
    { 
     using (Stream stream = ex.Response.GetResponseStream()) 
     { 
      using(StreamReader reader = new StreamReader(stream)) 
      { 
       var message = reader.ReadToEnd(); 
      } 
     } 
    } 
} 
+0

谢谢,这很有用。虽然理想情况下,我想使用我的Web服务代理类,这将无法使用(因为流似乎被处置)。 –

+0

@MoeSisko我已经测试过wcf服务和客户端代理。它工作正常。这就是为什么我发布这个答案。 – Damith

+0

对不起,我的意思是我使用旧式的asmx web服务(非wcf)。我会将你的接受者标记为接受,因为它最接近满足我的要求。 –

1

您应该使用WebException.Response检索消息:

using (WebClient wc = new WebClient()) 
{ 
    try 
    { 
     string content = wc.DownloadString(url); 
    } 
    catch (WebException ex) 
    { 
     if (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.ServiceUnavailable) 
     { 
      message = ex.Response 
     } 
    } 
} 
+0

听起来不错,但看到我的编辑。 –

+0

请尝试使用WebClient,请参阅我更新的答案。 – BluesRockAddict

+0

这可能会起作用,但我无法更改Web服务调用代码,只是为了获得此功能。但尝试+1。 –