【问题标题】:How to retrieve custome error message in Web Client request?如何在 Web 客户端请求中检索自定义错误消息?
【发布时间】:2017-03-23 20:16:15
【问题描述】:

返回文件的服务器端代码

[HttpGet]
public HttpResponseMessage GetVersionList(string Credentials, string VersionNumber)
{
  try
  {
      string UserLoginStatus = objUtility.LoginUser(objUtility.GetUserName(Credentials), objUtility.GetPassWord(Credentials));

      if (UserLoginStatus == "OK")
      {
          var path = objUtility.GetFileName(VersionNumber);

          if (path != null)
          {
              return FileAsAttachment(path, VersionNumber + ".exe");
          }
          else
          {               
              return Request.CreateErrorResponse(HttpStatusCode.OK, "File Not Found.");
          }
      }
      else
      {
          // Return reason why it failed.
          // It could be not authorized user, Not Active user, UID/Password wrong etc. 
          // Store into UserLoginStatus variable and pass show to client
          return Request.CreateResponse<string>(HttpStatusCode.OK, UserLoginStatus);
      }
  }
  catch (System.Exception ex)
  {
      return Request.CreateResponse<string>(HttpStatusCode.OK, ex.Message);
  }

}

// Win app code to download file 
using (WebClient webClient = new WebClient())
{  
            try
            {
                webClient.DownloadFile(serverUrl, (txtPath.Text + "\\" + txtVersion.Text + ".exe"));
            }
            catch (WebException wex)
            {
                // Show error message here 
            }
        }
    }
    catch (Exception ex)
    {
        lblStatus.Text = ex.InnerException + " <br> " + ex.Message;
    }
}

【问题讨论】:

    标签: c# asp.net-web-api asp.net-web-api2 windows-applications


    【解决方案1】:

    这就是我最近为 POS 编写的休息服务的做法。 (对于您实际使用的任何类,myErrorType 只是一个假人......)

    using (var response = request.GetResponse())
                    {   
     using (var reader = new StreamReader(response.GetResponseStream()))
                                {
                                    var reply = reader.ReadToEnd();
                                    myErrorType result = Newtonsoft.Json.JsonConvert.DeserializeObject<myErrorType>(reply);
                                    return result.Status;
                                    // do something with the results
                                }
    }
    

    抛出自定义错误:

    myErrorType  errresponse = new myErrorType();//dummy class, create your own...
    throw new WebFaultException<myErrorType>(errresponse, HttpStatusCode.BadRequest);
    

    【讨论】:

    • 感谢特雷的评论。但是,我将从 Windows 应用程序调用服务器端函数。如果我能得到一些代码 sn-p 将会有很大帮助,如果你能提供的话。
    • 您还需要什么?您是否需要实际错误的服务器端代码?您的问题是“如何在 Web 客户端请求中检索客户错误消息?”......我相信我已经回答了......但我会更新我的答案。
    • 我实际上正在寻找,如果 Web API 像 return Request.CreateErrorResponse(HttpStatusCode.NotFound, "File Not Found on Server.");如何在 Windows 应用程序中检索 Catch 块中的文本消息?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    • 2015-04-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多