API可与POSTMAN一起正常使用,但不适用于C#WebClient

时间:2018-08-23 16:16:39

标签: c# json api postman webclient

Client Web API与POSTMAN可以正常工作,没有任何问题。为了通过POSTMAN使用API​​,我使用了以下JSON输入(通过POST请求)

{  
   "ErrorMessage":null,
   "ErrorStatus":false,
   "ValidationDetails":{  
      "APIReferenceNumber":null,
      "BankReferenceNumber":"Session1",
      "ValidatiorKey":"2405201816004012ed810ec33849068309418a3cea680a"
   },
   "onlineRequestFields":{  
      "ShopReferenceNumber":"session21",
      "SessionID":"",
      "ChannelCode":"Call",
      "MobileNumber":"5899555"
   },
   "NIC":"83928392X",
   "SelectedProduct":"500"
}

这正常工作,没有任何错误,API返回了预期的JSON响应。但是,当我尝试通过C#Web Client使用API​​时,出现以下错误消息响应,并通过API返回。 Invalid request type code

class CryptoConsumeService
{
    private static readonly string apiUrl = "http://192.125.125.183/ClientDeploy/Customerloan.svc";

    public static string EvaluateRating()
    {
        object input = new
        {
            ErrorMessage = "null",
            ErrorStatus = false,

            ValidationDetails = new {
                APIReferenceNumber = "null",
                BankReferenceNumber = "SessionID",
                ValidatiorKey = "2405201816004012ed810ec33849068309418a3cea680a",
            },
            onlineRequestFields = new {
                ShopReferenceNumber = "session21",
                SessionID = "",
                ChannelCode = "Call",
                MobileNumber = "5899555",
            },
            NIC = "83928392X",
            SelectedProduct = "500"                
        };
        string inputJson = (new JavaScriptSerializer()).Serialize(input);
        WebClient webClient = new WebClient();
        WebClient client = webClient;
        client.Headers["Content-type"] = "application/json";
        client.Encoding = Encoding.UTF8;
        string json = client.UploadString(apiUrl + "/EvaluateRating", inputJson);
        return json;
    }
}

当我发布SelectedProduct的错误值时,POSTMAN将返回相同的错误响应。例如SelectedProduct = "600",但是您可以看到,上面的C#Web客户端代码值设置与POSTMAN请求相同。谁能告诉我发生此事的原因是什么?

0 个答案:

没有答案