I am unable to send data from my client app to a web method, i dont actually know what i am doing wrong any help would be appreciate.
Client:
public void syncClient(Customer c)
{
try
{
var datos = new JavaScriptSerializer().Serialize(c);
DataContractJsonSerializer ser =
new DataContractJsonSerializer(typeof(string));
MemoryStream mem = new MemoryStream();
ser.WriteObject(mem, datos);
string data = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);
string url = store.ParentUrl + "Api/syncClientFromChild";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = @"application/json"; //set the content type to JSON
request.Method = "POST";
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
byte[] send = encoding.GetBytes(data);
request.ContentLength = send.Length;
using (Stream newStream = request.GetRequestStream())
{
newStream.Write(send, 0, send.Length);
}
WebResponse ws1 = request.GetResponse();
JavaScriptSerializer serializer = new JavaScriptSerializer();
ApiCtrlResponse response = serializer.Deserialize<ApiCtrlResponse>(serializer.Serialize(ws1.GetResponseStream()));
if (response.success == true)
{
c.Synced = true;
_customerService.UpdateCustomer(c);
}
}
catch (Exception A)
{
}
}
I believe thats fine, but the other method i always get null in the string i need to receive:
[System.Web.Services.WebMethod]
public ActionResult syncClientFromChild(string Customer)
{
// do something
}
What i am missing ? Thanks in advance !
use a Model to accept Http Post,try to not use string,like
public class ReceiveModel
{
string Customer;
}
public ActionResult syncClientFromChild(ReceiveModel model)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.