简体   繁体   English

Web API:远程服务器返回错误。 (500内部服务器错误。 仅在联机时,不在本地计算机中

[英]Web API : The remote server returned an error. (500) Internal Server Error. Only when online and not in Local Machine

Here's the issue. 这是问题。 I am using ASP.NET Web API integration with my project and I am storing the request URLs in the web.config . 我将ASP.NET Web API与我的项目集成在一起,并将请求URL存储在web.config In my code, I use these URL to fire requests to the Web API and send and receive the data. 在我的代码中,我使用这些URL向Web API发出请求,并发送和接收数据。 The URLs are working fine, that I can assure you. 网址可以正常运行,我可以向您保证。 The problem here is with this single URL. 这里的问题是单个URL。

Its working when I run both projects on local machine and the flow of data is proper. 当我在本地计算机上运行两个项目并且数据流正常时,它可以正常工作。 But when I test run it online after hosting, the same service fails to send me data. 但是当我测试托管后在线运行它时,同一服务无法向我发送数据。 Instead, I receive the following exception in the catch block : 相反,我在catch块中收到以下异常:

The remote server returned an error: (500) Internal Server Error. 远程服务器返回错误:(500)Internal Server Error。

I have already confirmed the following to be true and correct : 我已经确认以下是正确的:

  1. The connection strings in both projects. 两个项目中的连接字符串。
  2. No spelling mistakes. 没有拼写错误。
  3. No incorrect URL. 没有错误的网址。
  4. When debugging on local, it shows no error and proceeds successfully. 在本地调试时,它不会显示任何错误并成功进行。
  5. Perfectly working on local and receiving proper data, only not working online. 完美地在本地工作并接收适当的数据,但不能在线工作。

I also tried re-hosting. 我也尝试过重新托管。 But still the same. 但是还是一样。 Not sure what could be the issue here. 不知道这里可能是什么问题。

Here is the controller method code : 这是控制器方法代码:

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
}

This is the web.config URL: 这是web.config URL:

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

Also, I have error logs setup in the Web API side incase of a bad request or something like that, but no new record is inserted in this case. 另外,如果有错误的请求或类似的情况,我会在Web API端设置错误日志,但是在这种情况下,不会插入任何新记录。 So this is leaving me confused so as to where the issue is. 所以这让我感到困惑,因为问题出在哪里。

This is what the exception is : 这是例外:

System.Net.WebException was caught HResult=-2146233079 Message=The remote server returned an error: (500) Internal Server Error. 捕获到System.Net.WebException HResult = -2146233079消息=远程服务器返回错误:(500)内部服务器错误。
Source=System StackTrace: at System.Net.WebClient.UploadDataInternal(Uri address, String method, Byte[] data, WebRequest& request) at System.Net.WebClient.UploadData(Uri address, String method, Byte[] data) at System.Net.WebClient.UploadData(String address, Byte[] data) Source = System StackTrace:在System.Net.WebClient.UploadDataInternal(Uri地址,字符串方法,Byte []数据,WebRequest&request)在System.Net.WebClient.UploadData(Uri地址,String方法,Byte []数据)在System .Net.WebClient.UploadData(字符串地址,字节[]数据)

And this is how I serialized my json object as a string and send json to the web api, just to help: 这就是我将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);
    }
}

and I have cross domain enabled on the web api side. 并且我在Web API端启用了跨域。

Been quite a long time since I saw this. 自从我看到这个以来已经有很长时间了。

Now I was almost looking at every possibility that I thought of and was suggested here in the comments and the answer. 现在,我几乎在考虑我想到的所有可能性,并在评论和答案中提出了建议。

The next day when I tried this same thing, it worked smooth ! 第二天,当我尝试同样的事情时,工作顺利!

So that left me to conclude that maybe the issue was on the server side. 因此,我可以得出结论,问题可能出在服务器端。 I use Azure and on that particular day, it might be having some issues and probably that was causing the 500 : Internal Server Error. 我使用Azure,并且在特定的一天,它可能会出现一些问题,并且可能导致了500:内部服务器错误。 And it has worked fine ever since. 从那时起,它一直运行良好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 远程服务器返回错误:(500)Internal Server Error。 - The remote server returned an error: (500) Internal Server Error. 从Windows服务将数据发布到Web应用程序时出错。“远程服务器返回错误:(500)内部服务器错误。” - Error While posting Data to a web application from a windows service.“The remote server returned an error: (500) Internal Server Error.” System.Net.WebException:远程服务器返回错误:(500)内部服务器错误。 - System.Net.WebException: The remote server returned an error: (500) Internal Server Error. 远程服务器返回错误:(500)Internal Server Error。 GetRequest()C# - The remote server returned an error: (500) Internal Server Error. GetRequest() C# 远程服务器返回错误:(500)内部服务器错误Web服务 - The remote server returned an error: (500) Internal Server Error Web service 使用c#将图像发布到Web服务器(远程服务器返回错误(500)内部服务器错误) - Post Image to Web Server in c# (The remote Server returned an error (500) Internal Server Error) Mandrill : 远程服务器返回错误:(500) 内部服务器错误 - Mandrill : The remote server returned an error: (500) internal server error 远程服务器返回错误:(500)网站上的内部服务器错误 - The remote server returned an error: (500) Internal Server Error on website 处理远程服务器返回错误:(500)内部服务器错误 - Handling The remote server returned an error: (500) Internal Server Error 未处理WEBException:远程服务器返回错误:(500)内部服务器错误 - WEBException was unhandled: The remote server returned an error: (500) Internal Server Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM