Click here to Skip to main content
15,905,420 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting exception of The remote server returned an error. (500) Internal Server Error on trying to fetch the response data from the hosted Web Service in asp.net with c#. Hosted Web Service is accessible too.

The Request API is in json format as below:
<string xmlns="http://tempuri.org/">
{"Req_UserId":"Samuel.Jacob","Req_Channel_ID":"IB","Req_Reference_No":"IB120219100103"}
</string>


Response API content is as below:

{
   "AuthenticateSSOResponse":
 {
       "Reference_No": "IB120219100103", 
       "Token_No":  "120219100103256"
   }
}


What I have tried:

Code as below:
public string CallWebService()
         {

            string URL = ConfigurationManager.AppSettings["NeST_WSI_API"] != null ? ConfigurationManager.AppSettings["NeST_WSI_API"].ToString() : string.Empty;



          JavaScriptSerializer oSerializer = new JavaScriptSerializer();
          string ReqJSON = BuildReqJson();
          System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();

          HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
          request.Method = "POST";
          request.Accept = @"application/json";
          request.ContentType = @"application/json";
          Byte[] byteArray = encoding.GetBytes(ReqJSON);
          request.ContentLength = byteArray.Length;



          using (Stream stm = request.GetRequestStream())
          {
              stm.Write(byteArray, 0, byteArray.Length);

          }



          using (StreamReader responseReader = new StreamReader(request.GetResponse().GetResponseStream()))
          {

              //string jsonResponse = responseReader.ReadToEnd();
              string jsonResponse = "jsonResponse";

              if (jsonResponse != null)
              {


              }
          }

          return "";


      }
      #endregion
Posted
Updated 20-May-19 3:45am
v2
Comments
F-ES Sitecore 20-May-19 8:14am    
First thing first....does what you're sending match what is required? For a start your code only constructs json (that json itself might not be valid, you didn't post the code for it), not the xml that is to be wrapped around it. Use something like fiddler or wireshark to examine the request you're actually making to see what is in the body.

1 solution

I suspect a 500 status code would indicate a problem on the server-side, but not necessarily with the application itself but with the payload it's receiving. Some REST services (which consume JSON or XML) deserialize the content using libraries. If the data being passed into the request doesn't match with what is expected for that end-point, the REST service might throw a 500 because it could not correctly deserialize the payload.

If you're absolutely confident that the payload is correct, I'd recommend looking at log files at the server-side to determine what might be causing the problem (usually web-services will log if there's problems deserializing). If you don't have access to this then perhaps raise a ticket with the REST service support?

Before you do any of this, have you tried running your request through a tool like Postman[^] which lets you immediately view the request + response bodies? Perhaps the 500 is being returned with additional information in the response itself.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900