Pass Object Parameter from Winform to Web RRS feed

  • Question

  • I want to pass a class object from my web forms to web using httpwebrequest. I am able to pass a string value but not complex object type.

    Following is my code snippet:

    private void BtnSend_Click(object sender, EventArgs e)
            Employee employee = new Employee();
            employee.FirstName = "abc";
            employee.LastName = "xyz";            
            //Serialize the object into stream before sending it to the remote server
            MemoryStream memmoryStream = new MemoryStream();
            BinaryFormatter binayformator = new BinaryFormatter();
            binayformator.Serialize(memmoryStream, employee);
            //var dataBytes = System.Text.Encoding.UTF8.GetBytes(employee.ToString());            
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:51953/Home/Contact");
            httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            httpWebRequest.ContentLength = memmoryStream.Length;
            //httpWebRequest.ContentLength = dataBytes.Length;
            httpWebRequest.Method = "POST";
            Stream dataStream = httpWebRequest.GetRequestStream();
            byte[] buffer = new byte[memmoryStream.Length];
            int count = memmoryStream.Read(buffer, 0, buffer.Length);
            dataStream.Write(buffer, 0, buffer.Length);
            //dataStream.Write(dataBytes, 0, dataBytes.Length);
            WebResponse response = httpWebRequest.GetResponse();
            //lblShow.Text = ((HttpWebResponse)response).StatusDescription;
            dataStream = response.GetResponseStream();
            StreamReader streamReader = new StreamReader(dataStream);
            string responseFromServer = streamReader.ReadToEnd();
            // Display the content.  
            //lblShow.Text += " " + responseFromServer;

    The commented code is for sending a string value which I can do, but now I want to pass a object type.

    On the web server code, I get values passed as null.

    Following is the snapshot: 

    Following is my Employee class:

    public class Employee
        public string FirstName { get; set; }
        public string LastName { get; set; }

    So any ways to pass object type from winforms to web using any approach.

    Any guidance or help would be great.

    Thanks In Advance!!!..

    Wednesday, June 19, 2019 4:50 PM

All replies

  • Hi Siddy Boy,

    Since you can transfer a separate string, maybe you can transfer the properties of the object in turn. And then use them to create new objects.



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Thursday, June 20, 2019 9:43 AM