Google Translate V2 cannot hanlde large text translations from C#

后端 未结 3 789
一生所求
一生所求 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: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", "

    Hello World

    " } }; try { var responseBytes = webClient.UploadValues(GoogleTranslateApiUrl, "POST", data); var json = Encoding.UTF8.GetString(responseBytes); var result = JsonConvert.DeserializeObject(json); var translation = result.data.translations[0].translatedText; } catch (Exception ex) { loggingService.Error(ex.Message); } }

提交回复
热议问题