none
Post request to a java servlet and process HttpResponse using .net RRS feed

  • Question

  • Hi,

    could someone please assist with an example, how to post request to a java servlet and process HttpResponse using .Net.

    Thank you!!


    Jothe Rengarajan

    Saturday, January 30, 2016 9:46 AM

Answers

  • Hi Jothe,

    In order to implement your requirement, I have created the following example, please try to refer to:

               // Create a request using a URL that can receive a post. 
                WebRequest request = WebRequest.Create ("your java servlet URL ");
                // Set the Method property of the request to POST.
                request.Method = "POST";
                // Create POST data and convert it to a byte array.
                string postData = "This is a test that posts this string to a Web server.";
                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 ();
                // Display the status.
                Console.WriteLine (((HttpWebResponse)response).StatusDescription);
                // Get the stream containing content returned by the server.
                dataStream = response.GetResponseStream ();
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader (dataStream);
                // Read the content.
                string responseFromServer = reader.ReadToEnd ();
                // Display the content.
                Console.WriteLine (responseFromServer);
                // Clean up the streams.
                reader.Close ();
                dataStream.Close ();
                response.Close ();
            // you could process your business logic here
    
    Best Regards,
    Amy Peng



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 1, 2016 11:20 AM
    Moderator

All replies

  • Hi Jothe,

    In order to implement your requirement, I have created the following example, please try to refer to:

               // Create a request using a URL that can receive a post. 
                WebRequest request = WebRequest.Create ("your java servlet URL ");
                // Set the Method property of the request to POST.
                request.Method = "POST";
                // Create POST data and convert it to a byte array.
                string postData = "This is a test that posts this string to a Web server.";
                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 ();
                // Display the status.
                Console.WriteLine (((HttpWebResponse)response).StatusDescription);
                // Get the stream containing content returned by the server.
                dataStream = response.GetResponseStream ();
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader (dataStream);
                // Read the content.
                string responseFromServer = reader.ReadToEnd ();
                // Display the content.
                Console.WriteLine (responseFromServer);
                // Clean up the streams.
                reader.Close ();
                dataStream.Close ();
                response.Close ();
            // you could process your business logic here
    
    Best Regards,
    Amy Peng



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 1, 2016 11:20 AM
    Moderator
  • thank you!! It worked for me.

    Jothe Rengarajan

    Friday, February 5, 2016 1:27 PM