Using HTTP Post · Server Code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyWebApp.projects.asmx
{
    public partial class UsingHTTPPost : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Translatev2Button_Click(object sender, EventArgs e)
        {
            if (EnglishTextBox.Text != string.Empty)
            {
                try
                {
                    // this is the content of the request
                    byte[] content = System.Text.Encoding.ASCII.GetBytes(
                        "english=" + EnglishTextBox.Text);

                    // create the request
                    System.Net.WebRequest request = System.Net.HttpWebRequest.Create(
                        "http://rodansotto.com/asmx/TranslateToFrenchService.asmx/TranslateToFrench");
                    request.Method = "POST";
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.ContentLength = content.Length;

                    // write the content to the request stream
                    System.IO.Stream requestStream = request.GetRequestStream();
                    requestStream.Write(content, 0, content.Length);
                    requestStream.Flush(); 

                    // get the response
                    System.Net.WebResponse response = request.GetResponse();                  
                    // get the stream associated with the response
                    System.IO.Stream responseStream = response.GetResponseStream();
                    // pipes the stream to a higher level stream reader with the 
                    //  required encoding format
                    System.IO.StreamReader streamReader = 
                        new System.IO.StreamReader(
                            responseStream, System.Text.Encoding.UTF8);

                    // the response is in XML format and normally needs to be handled
                    // but since the XML response is simple enough that when displayed
                    //  by the browser, it only displays what we need to display,
                    //  the french text
                    FrenchLabel.Text = streamReader.ReadToEnd();
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }

    }
}