【问题标题】:Web API : The remote server returned an error. (500) Internal Server Error. Only when online and not in Local MachineWeb API:远程服务器返回错误。 (500内部服务器错误。仅当在线且不在本地计算机中时
【发布时间】:2015-05-19 05:52:33
【问题描述】:

这就是问题所在。我正在将 ASP.NET Web API 与我的项目集成,并将请求 URL 存储在 web.config 中。在我的代码中,我使用这些 URL 向 Web API 发出请求并发送和接收数据。我可以向您保证,这些 URL 工作正常。这里的问题在于这个单一的 URL。

当我在本地机器上运行这两个项目并且数据流正确时,它的工作原理。但是当我在托管后在线测试运行它时,相同的服务无法向我发送数据。相反,我在 catch 块中收到以下异常:

远程服务器返回错误:(500) Internal Server Error.

我已经确认以下是真实和正确的:

  1. 两个项目中的连接字符串。
  2. 没有拼写错误。
  3. 没有错误的 URL。
  4. 在本地调试时,没有报错,继续成功。
  5. 完美地在本地工作并接收正确的数据,只是不能在线工作。

我也尝试过重新托管。但还是一样。不知道这里可能是什么问题。

这里是控制器方法代码:

using (var client = new WebClient())
{
   Model serviceModel = new Model();
   serviceModel.Number = Num;
   serviceModel.type = "getalldetails";
   client.Headers[HttpRequestHeader.ContentType] = "application/json";
   JavaScriptSerializer serializer = new JavaScriptSerializer();
   var result = client.UploadData(System.Web.Configuration.WebConfigurationManager.AppSettings["GetDetails"], Encoding.UTF8.GetBytes(serializer.Serialize(serviceModel)));//this is the exception point
}

这是web.config 网址:

<add key="GetDetails" value="http://hostname/api/controller/GetDetails" />

此外,我在 Web API 端设置了错误日志,以防出现错误请求或类似情况,但在这种情况下没有插入新记录。所以这让我对问题出在哪里感到困惑。

这是例外情况:

System.Net.WebException 被捕获 HResult=-2146233079 Message=The 远程服务器返回错误:(500) 内部服务器错误。
源 = 系统堆栈跟踪: 在 System.Net.WebClient.UploadDataInternal(Uri 地址,String 方法,Byte[] 数据,WebRequest& 请求) 在 System.Net.WebClient.UploadData(Uri 地址,String 方法,Byte[] 数据) 在 System.Net.WebClient.UploadData(String address, Byte[] data)

【问题讨论】:

  • 你确定 serviceModel.type 是小写字母吗?
  • 是的。我已经仔细检查了参数。它们都是正确的。事实上,它也在本地机器上工作。
  • 您是否使用 postman 或类似工具手动尝试过 API?
  • 我刚刚遇到了完全相同的问题,但是我使用了带有 json 的 UploadString,问题出在我的 json 字符串上,所以我认为您的问题出在字节数组上。
  • 如果您在生产环境中将 API 项目的 web.config 中的自定义错误“打开”,您是否得到任何其他详细信息?它仅在 localhost 中有效的事实可能表明缺少对服务器的依赖?

标签: c# asp.net serialization asp.net-web-api


【解决方案1】:

这就是我如何将我的 json 对象序列化为字符串并将 json 发送到 web api,只是为了帮助:

//Get the json
private JsonResult GetJsonResult()
{
    //whatever json you are expecting on api side
    return Json(new { Number = "Num", type = "getalldetails" });
}

private void Update()
{
    var URI = new Uri("http://hostname/api/controller/GetDetails");
    //serialize json object to string, .Data just to get json data
    string jsonText = JsonConvert.SerializeObject(GetJsonResult().Data);

    //post to api
    using (var client = new WebClient())
    {
        client.Encoding = Encoding.UTF8;
        client.Headers.Add("Content-Type", "text/json");
        client.UploadString(URI, "POST", jsonText);
    }
}

我在 web api 端启用了跨域。

【讨论】:

    【解决方案2】:

    好久没看到了。

    现在我几乎正在研究我想到的每一种可能性,并在 cmets 和答案中被建议。

    第二天我尝试了同样的事情,它运行顺利!

    这让我得出结论,可能问题出在服务器端。我使用 Azure,在那一天,它可能有一些问题,可能是导致 500 : Internal Server Error 的原因。从那以后它一直运行良好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多