Пример #1
0
 /// <summary>
 /// 获取服务端版本文件  失败抛出异常
 /// </summary>
 /// <param name="url">请求地址</param>
 /// <returns>版本文件的Xml内容</returns>
 public static string GetUpgradeList(string url)
 {
     try
     {
         HttpWebClient client = new HttpWebClient();
         client.Encoding = System.Text.Encoding.UTF8;
         url             = string.Format("{0}/Upgrade/UpgradeList", url);
         return(client.DownloadString(url));
     }
     catch (WebException ex)
     {
         //响应不为空异常时,获取服务器返回的错误信息
         if (ex.Response != null)
         {
             Stream       stream = ex.Response.GetResponseStream();
             StreamReader reader = new StreamReader(stream);
             string       result = reader.ReadToEnd();
             throw new Exception(result);
         }
         else
         {
             Exception baseEx = ex.GetBaseException();
             if (baseEx != null)
             {
                 throw baseEx;
             }
             else if (ex.InnerException != null)
             {
                 throw ex.InnerException;
             }
             else
             {
                 throw ex;
             }
         }
     }
     catch (Exception e)
     {
         throw e;
     }
 }
Пример #2
0
 /// <summary>
 /// 同步下载文件  失败抛出异常
 /// </summary>
 /// <param name="hostUrl"></param>
 /// <param name="fileName"></param>
 /// <param name="localFileName"></param>
 public static void DownloadFile(string hostUrl, string fileName, string localFileName)
 {
     try
     {
         HttpWebClient client = new HttpWebClient();
         string        url    = string.Format("{0}/Upgrade/DownloadFile?fileName={1}", hostUrl, HttpUtility.UrlEncode(fileName));
         client.DownloadFile(url, localFileName);
     }
     catch (WebException ex)
     {
         //响应不为空异常时,获取服务器返回的错误信息
         if (ex.Response != null)
         {
             Stream       stream = ex.Response.GetResponseStream();
             StreamReader reader = new StreamReader(stream);
             string       result = reader.ReadToEnd();
             throw new Exception(result);
         }
         else
         {
             Exception baseEx = ex.GetBaseException();
             if (baseEx != null)
             {
                 throw baseEx;
             }
             else if (ex.InnerException != null)
             {
                 throw ex.InnerException;
             }
             else
             {
                 throw ex;
             }
         }
     }
     catch (Exception e)
     {
         throw e;
     }
 }