behzad
behzad

Reputation: 196

send object via webrequest and receive in web service

i want to send object to a application and receive same object from that . i want to send and recieve a message class between 2 application . here is my client code :

[Serializable]
    public struct Messages
    {
        public string ErrorCode { get; set; }

        public string ErrorMessage { get; set; }

        public MessageType? messageType { get; set; }

        public SendMessageType? sendMessageType { get; set; }

        public string UserCode { get; set; }

        public DateTime MessageDateTime { get; set; }

        public MethodName methodName { get; set; }

        public int? FieldsCount { get; set; }

        public List<KeyValue> Values { get; set; }
    }

    public struct KeyValue
    {
        public string key { get; set; }

        public string value { get; set; }
    }

    public enum MessageType
    {
        Request,
        Reply,
        Reject
    }

    public enum MethodName
    {
        FindShebaFromDeposit = 1,
        BalanceInquery = 2,
        CheckBill = 3,
        BillPayment = 4,
        GetDepositList = 5,
        NormalTransfer = 6,
        GetPersonInfoFromDeposit = 7
    }

    public enum SendMessageType
    {
        way2,
        way3
    }

    public void POST(Messages messages)
    {
        //Serialize the object into stream before sending it to the remote server
        MemoryStream memmoryStream = new MemoryStream();
        BinaryFormatter binayformator = new BinaryFormatter();
        binayformator.Serialize(memmoryStream, messages);

        //Cretae a web request where object would be sent
        WebRequest objWebRequest = WebRequest.Create("http://localhost:13060/Default/Index");
        objWebRequest.Method = "POST";
        objWebRequest.ContentLength = memmoryStream.Length;
        // Create a request stream which holds request data
        Stream reqStream = objWebRequest.GetRequestStream();
        //Write the memory stream data into stream object before send it.
        byte[] buffer = new byte[memmoryStream.Length];
        int count = memmoryStream.Read(buffer, 0, buffer.Length);
        reqStream.Write(buffer, 0, buffer.Length);

        //Send a request and wait for response.
        try
        {
            WebResponse objResponse = objWebRequest.GetResponse();
            //Get a stream from the response.
            Stream streamdata = objResponse.GetResponseStream();
            //read the response using streamreader class as stream is read by reader class.
            StreamReader strReader = new StreamReader(streamdata);
            string responseData = strReader.ReadToEnd();
        }
        catch (WebException ex)
        {
            throw ex;
        }
    }

and here is my server code :

public class DefaultController : Controller
{
    // GET: Default
    [HttpPost]
    public Bussiness.Message.CoreMessage Index(Bussiness.Message.CoreMessage coreMessage)
    {
        Puller.Bussiness.ServiceMessages serviceMessage = new Bussiness.ServiceMessages();
        var file = serviceMessage.CreateRequest(coreMessage);
        return file;
    }
}

message class serilize and send to server but in server it is null?

Upvotes: 0

Views: 1537

Answers (1)

Adnan Umer
Adnan Umer

Reputation: 3689

You are encoding your request body in binary format, that will not be deserialized directly. Better is to send request body in JSON format that will be by-default deserialized by ASP.NET MVC.

To serialize to JSON you can use variety of options. I'm using JavaScriptSerializer.Serialize for that.

Alternatively you can use JsonConvert.SerializeObject, DataContractJsonSerializer or any other library of your choice to serialize to json.

public void POST(Messages messages)
{
    var request = (HttpWebRequest)WebRequest.Create("http://localhost:13060/Default/Index");
    request.ContentType = "application/json";
    request.Method = "POST";

    using (var streamWriter = new StreamWriter(request.GetRequestStream()))
    {
        string json = new JavaScriptSerializer().Serialize(messages);
        streamWriter.Write(json);
    }

    var response = (HttpWebResponse)request.GetResponse();
    using (var streamReader = new StreamReader(response.GetResponseStream()))
    {
        var result = streamReader.ReadToEnd();
    }
}

Upvotes: 2

Related Questions