C#Restsharp - 如何验证内容结果是意外结果

时间:2016-08-04 09:10:48

标签: c# restsharp

获取内容结果的代码

IRestResponse response = client.Execute(request);
var content = (string)JsonConvert.DeserializeObject(response.Content);

response.content应返回此4 msg中的以太一个。

  • INVALID_MESSAGE_ID
  • MESSAGE_NOT_FOUND
  • INTERNAL_ERROR

如果API返回其他内容怎么办?如何验证?

1 个答案:

答案 0 :(得分:0)

检查内容:

if(content != "OK" && content != "INVALID_MESSAGE_ID" && content != "MESSAGE_NOT_FOUND" && content != "INTERNAL_ERROR") {
    //Do some handling, like logging
}

或者:

switch(content) {
    case "OK":
        // Do something, when content is OK
        break;
    case "INVALID_MESSAGE_ID":
        // Do something, when content is INVALID_MESSAGE_ID
        break;
    case "MESSAGE_NOT_FOUND":
        // Do something, when content is MESSAGE_NOT_FOUND
        break;
    case "INTERNAL_ERROR":
        // Do something, when content is INTERNAL_ERROR
        break;
    case default:
        // Do something, if no case is matched
        break;
}