Google Translate V2 cannot hanlde large text translations from C#

后端 未结 3 790
一生所求
一生所求 2020-12-11 04:56

I\'ve implemented C# code using the Google Translation V2 api with the GET Method. It successfully translates small texts but when increasing the text length and it takes 1,

相关标签:
3条回答
  • 2020-12-11 05:35

    ? What? it is trivial to post using C# - it is right there in the documentation.

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
    {
        // Set type to POST
        request.Method = "POST";
    

    From there on you bascially put the data into fom fields into the content stream.

    This is not "simulate a post meethod", it is fully doing a post request as per specifications.

    Btw. hwhere does json enter here? You say "in C#". There is no need to use json?

    0 讨论(0)
  • 2020-12-11 05:53

    The accepted answer appears to be out of date. You can now use the WebClient (.net 4.5) successfully to POST to the google translate API making sure to set the X-HTTP-Method-Override header.

    Here is some code to show you how.

    using (var webClient = new WebClient())
    {
        webClient.Headers.Add("X-HTTP-Method-Override", "GET");
        var data = new NameValueCollection() 
        { 
            { "key", GoogleTranslateApiKey }, 
            { "source", "en" }, 
            { "target", "fr"}, 
            { "q", "<p>Hello World</p>" } 
        };
        try
        {
            var responseBytes = webClient.UploadValues(GoogleTranslateApiUrl, "POST", data);
            var json = Encoding.UTF8.GetString(responseBytes);
            var result = JsonConvert.DeserializeObject<dynamic>(json);
            var translation = result.data.translations[0].translatedText;
        }
        catch (Exception ex)
        {
            loggingService.Error(ex.Message);
        }
    } 
    
    0 讨论(0)
  • 2020-12-11 05:55

    Apparently using WebClient won't work as you cannot alter the headers as needed, per the documentation:

    Note: You can also use POST to invoke the API if you want to send more data in a single request. The q parameter in the POST body must be less than 5K characters. To use POST, you must use the X-HTTP-Method-Override header to tell the Translate API to treat the request as a GET (use X-HTTP-Method-Override: GET).

    You can use WebRequest, but you'll need to add the X-HTTP-Method-Override header:

    var request = WebRequest.Create (uri);
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.Headers.Add("X-HTTP-Method-Override", "GET");
    
    var body = new StringBuilder();
    body.Append("key=SECRET");
    body.AppendFormat("&source={0}", HttpUtility.UrlEncode(source));
    body.AppendFormat("&target={0}", HttpUtility.UrlEncode(target));
     //--
    body.AppendFormat("&q={0}", HttpUtility.UrlEncode(text));
    
    var bytes = Encoding.ASCII.GetBytes(body.ToString());
    if (bytes.Length > 5120) throw new ArgumentOutOfRangeException("text");
    
    request.ContentLength = bytes.Length;
    using (var output = request.GetRequestStream())
    {
        output.Write(bytes, 0, bytes.Length);
    }
    
    0 讨论(0)
提交回复
热议问题