2

Background:

I've been requested to create a translation web service which basically must interact with Google Translate apis. Since the text I do need to translate is larger than 2,000 characters I cannot use the GET WebRequest/GET method. Instead, I should use the POST which supports till 5,000 characters.

Tech Notes:

VS 2010 Utlimate, Google Translate API v2 (http://code.google.com/apis/language/translate/v2/using_rest.html#WorkingResults)

Issue:

I've implemented below code for the POST web request and it works for URLs having a max of 1,489 characters. If the the URL contains 1,490 then an exception is raised: "The remote server returned an error: (414) Request-URI Too Large."

 using System;
        using System.Collections.Generic;
        using System.IO;
        using System.Net;
        using System.Runtime.Serialization;
        using System.Runtime.Serialization.Json;
        using System.Text;
        using Newtonsoft.Json.Linq;


        string sourceLanguage = "es"; // English
        string targetLanguage = "en"; // Spanish
        string text = "Working from home ( please replace this string for a text longer thant 1,490 characters!!! )"; // Text to be translated

        // url for a web request
        String apiUrl = "https://www.googleapis.com/language/translate/v2?key={0}&source={1}&target={2}&q={3}";
        String url = String.Format(apiUrl, Constants.apiKey, sourceLanguage, targetLanguage, text);

        byte[] bytes = Encoding.ASCII.GetBytes(url);
        Stream outputStream = null;
        string translatedText = string.Empty;

        // web request
        WebRequest webRequest = WebRequest.Create(url);

        // POST method
        webRequest.Method = "POST";
        webRequest.Headers.Add("X-HTTP-Method-Override:GET");
        webRequest.ContentType = "application/x-www-form-urlencoded";

        try
        {
            // send the POST
            webRequest.ContentLength = bytes.Length;
            outputStream = webRequest.GetRequestStream();
            outputStream.Write(bytes, 0, bytes.Length);
        }
        catch (WebException e)
        {
            Console.WriteLine(e.Message, "HttpPost: Request error");
        }

        try
        {

            // get the response
            WebResponse webResponse = webRequest.GetResponse();

            if (webResponse != null)
            {
                // read response stream 
                using (StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
                {

                    JObject obj = JObject.Parse(sr.ReadToEnd());
                    JToken translated = obj.SelectToken("data.translations[0].translatedText");
                    // get translated text into a string
                    translatedText = translated.Value<string>();
                }
            }
        }
        catch (WebException e)
        {
            Console.WriteLine(e.Message, "HttpPost: Response error");
        }

Question:

Does anybody have experience using this translation API and see which is the error here? Can anyone tell me which is the f"$@! error I spent the last 3 hrs looking for!! I'm going crazy with this. Any contribution will be REALLY appreciated. Thanks in advance! :)

Important Note: I was able to overcome the POST issue by following this Post: Google Translate V2 cannot hanlde large text translations from C#

Community
  • 1
  • 1
G21
  • 1,297
  • 2
  • 18
  • 39
  • Please include the answer to your question as an answer. It's just a better way of doing things – Sheena Mar 05 '13 at 10:33

1 Answers1

1

Since you're overriding the GET, you should be able to put the parameters in your request body instead of on the query string. The error you're receiving is perfectly valid if you're passing in a large amount of text. I don't think the RFC specifies a maximum limit, but does provide the 414 error code for the server to be able to respond appropriately.

Robaticus
  • 22,857
  • 5
  • 54
  • 63
  • Can you please be clearer? I've not found the correct way to successfully override the GET method. I'm still facing the "URI too large" error on URI lenghts of 1800 chars :S – G21 Feb 24 '12 at 14:21