Click here to Skip to main content
15,904,822 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am getting
C#
The remote server returned an error: (500) Internal Server Error.


while posting data to web api from Asp.net:my code is below.
asp.net button code:
C#
protected void Button3_Click(object sender, EventArgs e)
  {
      var request = (HttpWebRequest)WebRequest.Create("http://23.92.223.177:8888/api/Contact/AddContact");

      var postData = "BusinessAccount=TESTBUS";
      postData += "&FirstName=zz&LastName=zz&Position=112&Phone=0009&Email=b1@b1!com";
      var data = Encoding.ASCII.GetBytes(postData);

      request.Method = "POST";
       request.ContentType = "application/x-www-form-urlencoded";
    //  request.ContentType = "application/json; charset=utf-8";
      request.ContentLength = data.Length;

      using (var stream = request.GetRequestStream())
      {
          stream.Write(data, 0, data.Length);
      }

      var response = (HttpWebResponse)request.GetResponse();

      var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

}

and Web Api code:
C#
[System.Web.Http.HttpPost]
     [System.Web.Http.ActionName("AddContact")]
     public IEnumerable<CUDResponse> AddContact(Contact contact)
     {
         string param1 = contact.BusinessAccount, param2 = contact.FirstName, param3 = contact.LastName, param4 = contact.Position, param5 = contact.Phone, param6 = contact.Email, param7 = "US";
         try
         {
             param6 = param6.ToString().Replace('!', '.');
             AcumaticaCRUD objcrud = new AcumaticaCRUD();

             int ContactId = GetContactId(ConfigurationManager.AppSettings["IP"].ToString(), ConfigurationManager.AppSettings["Port"].ToString(), ConfigurationManager.AppSettings["CompanyName"].ToString(), ConfigurationManager.AppSettings["AcumaticaLoginUserName"].ToString(), ConfigurationManager.AppSettings["AcumaticaPassword"].ToString(), param6);
             string IsBusinessAccountExist = objcrud.IsBusinessAccountExistinAcumatica(param1);
             if (ContactId == 0 && !string.IsNullOrEmpty(IsBusinessAccountExist))
             {
                 bool? InsertContact = objcrud.InsertContactinAcumatica(param1, param2, param3, param4, param5, param6, param7);

                 if (InsertContact == true)
                 {
                     // inserted
                     CUDList.Add(new CUDResponse { IsSuccess = 1 });
                 }
                 else
                 {
                     // not inserted
                     CUDList.Add(new CUDResponse { IsSuccess = 0 });
                 }
             }
             else if (ContactId != 0)
             {
                 //already Exists   // checked Is Exist by emailID /// if exist then comes here
                 CUDList.Add(new CUDResponse { IsSuccess = -1 });
             }
             else if (string.IsNullOrEmpty(IsBusinessAccountExist))
             {
                 // If Business Account not in the Acumatica
                 CUDList.Add(new CUDResponse { IsSuccess = -2 });
             }

             return CUDList;
         }
         catch (Exception ex)
         {
             Logger<ContactController>.LogException(ex);
             throw;
         }
     }



please help i try a lot but i don't know what is the problem.
Posted
Comments
F-ES Sitecore 12-Jan-16 9:09am    
Use a tool like Fiddler to examine the request and response at the network level, the body of the response might have more detailed info about what the problem is.

1 solution

You are getting 500 error because web api method signature is not matching with the arguments sent by web request object.

You need to send the person details to the web api method. You can use

C#
var request = (HttpWebRequest)WebRequest.Create("http://23.92.223.177:8888/api/Contact/AddContact");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "Here you can set JSON data..";
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream ();
// Write the data to the request stream.
dataStream.Write (byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close ();
// Get the response.
WebResponse response = request.GetResponse ();

byte[] byteArray = Encoding.UTF8.GetBytes (postData);


for more details you can check c# - How to use WebRequest to POST some data and read response? - Stack Overflow[^]
 
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