<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<div>
<asp:updatepanel id="UpdatePanel12" runat="server">
<ContentTemplate>
<div style="position: relative; width: 324px; margin-left: 20px; margin-top:20px;
margin-bottom:20px;">
<asp:Label ID="lblInstructions" runat="server" Width="400px" Text="Enter some text for the language
you want translated:"></asp:Label>
</div>
<!-- Textbox for the input text to be translated -->
<div style="position: relative; width: 324px; margin-left:20px; margin-bottom:10px;">
<asp:TextBox ID="txtInput" runat="server" Width="359px" Height="200px"
TextMode="MultiLine"></asp:TextBox>
</div>
<!-- Button with click event to translate the language of the text in the txtInput textbox -->
<div style="position: relative; width: 167px; margin-left:20px; margin-bottom:20px;">
<asp:Button ID="btnTranslate" runat="server" Text="Translate Text"
onclick="btnTranslate_Click" />
</div>
<!-- Label for the result of the translate method -->
<div style="position: relative; width: 324px; margin-left:20px; margin-bottom:10px;">
<asp:Label ID="lblOutput" runat="server" Width="602px"></asp:Label>
</div>
<fieldset>
<legend>Results</legend>
<table>
<tr>
<td>
Translating text is also avilable through Microsoft's Translator HTTP API, it is
achieved through a web request POST method. The language to translate to and
from need to be included in your web request URL, while the text to be translated
is extracted as plain text from the request body. In the example above you will
see in the Code-Behind that the to and from languages have been hard coded as
English to Spanish, this can be set dynamically through your own culture code
parameters.
</td>
</tr>
<tr />
<tr>
<td>
Try entering some english text in the box above and click the 'Translate Text'
button, your text will be sent to the HTTP API web service, translated to
Spanish and then displayed on the page for you. Once you have tried the demo
click the Code-Behind tab above to see exactly how this is achieved through c#.
</td>
</tr>
</table>
</fieldset>
</ContentTemplate>
</asp:updatepanel>
</div>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
namespace InteractiveSDK.HTTP
{
public partial class HTTPDemo2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
// Click event for the button
protected void btnTranslate_Click(object sender, EventArgs e)
{
// Setting the label text as the return value of the PostData() method
lblOutput.Text = PostData();
}
private string PostData()
{
// Creating a string with the request URL
// The URL includes the appId and in this case hardcoded to translate from
// English to Spanish
string translateUri =
"http://api.microsofttranslator.com/V1/Http.svc/Translate?appId=yourAppId&from=en&to=es";
// Create a web request to the URL
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(translateUri);
// Set the request method to POST
httpWebRequest.Method = "POST";
// Set the content type (needs to be text/plain for POST methods)
httpWebRequest.ContentType = "text/plain";
// Encode the txtInput value bytes for the POST
byte[] bytes = Encoding.ASCII.GetBytes(txtInput.Text);
// Create a stream for the request
Stream os = null;
try
{
// Attach the txtInput value to the request
httpWebRequest.ContentLength = bytes.Length;
os = httpWebRequest.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
}
finally
{
if (os != null)
{
// Close the stream when finished
os.Close();
}
}
// Send the request and get the response
WebResponse response = httpWebRequest.GetResponse();
// Open a stream for the response
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
// Create a string for the contents of the web response
string output = reader.ReadToEnd();
// Return the output string
return output;
}
}
}