【问题标题】:How to convert an API call from javascript to C#?如何将 API 调用从 javascript 转换为 C#?
【发布时间】:2020-05-09 23:36:37
【问题描述】:

如何在 C# 中进行 api 调用? javascript 代码作为 jsfiddle 工作,但不在我的控制台应用程序中

我有这段代码可以在 javascript 中运行,但在 c# 代码中却无法运行,我得到了 500

var jsoninput = { inputParams:
       JSON.stringify({
              SecurityCode: 'xxx123',
              StatusID: 'O',
              FromShiftDate: '01/16/2020',
              ToShiftDate: '01/22/2020',
              ExcludePerDiem: false,
              ExcludeContracts: false,
              ExcludePermPlacement: false
       }, null, 2)
};
var jqXHR = $.ajax({
       type: 'POST',
       url: 'https://example.com/api.asmx/Agency_ListOrdersByShiftDate',
       data: JSON.stringify(jsoninput),
       cache: false,
       async: true,
       crossDomain: true,
       contentType: 'application/json; charset=utf-8',
       dataType: 'json',
       success: function(data, textStatus, jqXHR) {
              var odata = JSON.parse(data.d);
              if (odata.success) {
                     alert(data.d);
                     //Do something
                     //Reference the orders directly: odata.data.Orders
                     //or access raw: data.d
              } else {
                    alert(data.d);
                     //do something with errors
               }
       },
       error: function(jqXHR, textStatus, errorThrown) {
            alert("Status : " + textStatus + "  Message : "+ errorThrown);
            //Error Handling
       }
});

试图让它在 Windows 控制台 c# 网络应用程序上工作,但得到 500 个代码

string apiUrl = "https://example.com/api.asmx/Agency_ListOrdersByShiftDate";

            var input = new
            {
              SecurityCode = "xxx123",
              StatusID = "O",
              FromShiftDate = "01/20/2020",
              ToShiftDate = "01/30/2020",
              ExcludePerDiem = false,
              ExcludeContracts = false,
              ExcludePermPlacement = false
            };

            string inputJson = (new JavaScriptSerializer()).Serialize(input);
            WebClient client = new WebClient();


            client.Headers["Content-type"] = "application/json";
            client.Encoding = Encoding.UTF8;
            string json = client.UploadString(apiUrl, inputJson);

我已经试过了

 var httpClient = new HttpClient();
 var result = await httpClient.PostAsync(apiUrl, input.AsJson());

结果:

StatusCode:500,ReasonPhrase:“内部服务器错误”,版本:1.1,内容:System.Net.Http.StreamContent,标头: { json错误:真 缓存控制:私有 日期:格林威治标准时间 2020 年 1 月 23 日星期四 17:23:06 服务器:Microsoft-IIS/10.0 X-AspNet-版本:2.0.50727 X-Powered-By: ASP.NET 内容长度:91 内容类型:应用程序/json;字符集=utf-8 }

【问题讨论】:

  • 您是否尝试过将完全相同的输入参数传递给工作版本?
  • 看来它们是不同的端点。首先尝试完全相同。
  • 另外:" 重要我们不建议您使用 WebClient 类进行新开发。而是使用 System.Net.Http.HttpClient 类。" @987654321 @
  • edit您的问题以改进它或添加信息。评论不是一个好地方。
  • 调试inputJson并检查你的json是否有效

标签: javascript c# .net api


【解决方案1】:

必须创建 LastByShiftParams 的对象,将其序列化为另一个对象 LinkInParams 中 inputParams 的属性,然后再序列化该对象

var jsoninput = new LinkInParams(new LastByShiftParams(apiKey, "01/21/2020", "01/29/2020"));


         public LinkInParams(object inObject)
                {
                    inputParams = JsonConvert.SerializeObject(inObject, Formatting.Indented);
                }  

      class LastByShiftParams
        {  
     public string SecurityCode ;
            public string StatusID ;
            public string FromShiftDate ;
            public string ToshiftDate ;
            public bool ExcludePerDiem ;
            public bool ExcludeContracts ;
            public bool ExcludePermPlacement;
        }

【讨论】:

    猜你喜欢
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    • 2020-07-21
    • 1970-01-01
    • 2015-10-25
    • 2013-07-27
    • 2012-01-13
    • 1970-01-01
    相关资源
    最近更新 更多