Export (0) Print
Expand All
144 out of 187 rated this helpful - Rate this topic

Using the HTTP Interface

The HTTP interface allows applications to integrate translation functionality by invoking HTTP GET & POST methods. The interface is technology agnostic; no particular operating system or programming language is needed to develop using it. The HTTP API uses the same protocols and verbs as the World Wide Web and is simpler than AJAX.

In HTTP interface all method's parameter values can be send using query string or as a POST data. To learn how to use HTTP interface of Microsoft translator we are considering translate method here.

To make request to any method of Microsoft translator you will need access token. Process of obtaining access token is explained in detailed at Obtain Access Token. Use the value of access token in the Authorization header of the subsequent calls to the Microsoft Translator API. This Token is valid for 10 minutes. Authorization header value should be in following format. (Note: One BLANK space between Bearer and Access Token Value)

"Bearer" + " " + Access Token Value

In C#, you can use HttpWebRequest to make request.


                                
string text = "Use pixels to express measurements for padding and margins.";
string from = "en";
string to = "de";

string uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + System.Web.HttpUtility.UrlEncode(text) + "&from=" + from + "&to=" + to;
string authToken = "Bearer" + " " + admToken.access_token;

HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
httpWebRequest.Headers.Add("Authorization", authToken);

                            

In this code snippet, parameters required for translate method are provided through URL as a query string parameters.

Making HTTP request to Translate method

The code snippet below makes a request to the Microsoft Translator's Translate method.


                                
WebResponse response = null;
try
{
   response = httpWebRequest.GetResponse();
   using (Stream stream = response.GetResponseStream())
   {
     System.Runtime.Serialization.DataContractSerializer dcs = new System.Runtime.Serialization.DataContractSerializer(Type.GetType("System.String"));
     string translation = (string)dcs.ReadObject(stream);
     Console.WriteLine("Translation for source text '{0}' from {1} to {2} is", text, "en", "de");
     Console.WriteLine(translation);
   }
}
                            

For all available HTTP method reference

Show:
© 2014 Microsoft. All rights reserved.