0
votes

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. In my code, I use these URL to fire requests to the Web API and send and receive the data. The URLs are working fine, that I can assure you. The problem here is with this single 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 :

The remote server returned an 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:

<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. 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.
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)

2
Are you sure serviceModel.type is small letter? - Mindless
Yes. I have double checked the parameters. They are all proper. Infact, its working on local machine too. - Matt Murdock
Did you try the API manually with postman or similar tool? - Yooz
I just had the exact same problem, but i used UploadString with json, the problem was with my json string, so I think your problem is with your byte array. - Mindless
If you turn the custom error "On" in the web.config of the API project in production, do you get any other details? The fact that it works only in localhost could suggest a missing dependency on the server? - Yooz

2 Answers

0
votes

And this is how I serialized my json object as a string and send json to the web api, just to help:

//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.

0
votes

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. And it has worked fine ever since.