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

查看:1087
本文介绍了远程服务器返回错误:(500)内部服务器错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在

The remote server returned an error: (500) Internal Server Error.

<从Asp.net发送数据到web api时我的代码如下:

asp.net按钮代码:



while posting data to web api from Asp.net:my code is below.
asp.net button code:

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();



}



和网络Api代码:


}

and Web Api code:

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

推荐答案

由于web api方法签名与web请求对象发送的参数不匹配,因此收到500错误。



您需要将此人详细信息发送到web api方法。您可以使用



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

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





有关详细信息,请查看 c# - 如何使用WebRequest来发布一些数据并读取响应? - 堆栈溢出 [ ^ ]


这篇关于远程服务器返回错误:(500)内部服务器错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆