如何在 Web 客户端请求中检索自定义错误消息?
How to retrieve custome error message in Web Client request?
Server side code to return file
[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;
}
}
我最近为POS写的休息服务就是这样做的。 (myErrorType 只是你实际使用的 class 的虚拟对象...)
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);
Server side code to return file
[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;
}
}
我最近为POS写的休息服务就是这样做的。 (myErrorType 只是你实际使用的 class 的虚拟对象...)
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);