【问题标题】:Google Translator API "Too Large URL"Google Translator API“网址太大”
【发布时间】:2013-11-15 07:21:45
【问题描述】:

我最近开始研究 Translator API,当我的输入文本超过 1300 个字符时,我收到错误 Too Large URL。

我正在使用下面的代码

        string apiKey = "My Key";
        string sourceLanguage = "en";
        string targetLanguage = "de";
        string googleUrl;
        string textToTranslate = 
        while (textToTranslate.Length < 1300)
        {
            textToTranslate = textToTranslate + " hello world ";
        }
        googleUrl = "https://www.googleapis.com/language/translate/v2?key=" + apiKey + "&q=" + textToTranslate + "&source=" + sourceLanguage + "&target=" + targetLanguage;

        WebRequest request = WebRequest.Create(googleUrl);
        // Set the Method property of the request to POST^H^H^H^HGET.
        request.Method = "GET"; // <-- ** You're putting textToTranslate into the query string so there's no need to use POST. **

        //// Create POST data and convert it to a byte array.
        //string postData = textToTranslate;
        //byte[] byteArray = Encoding.UTF8.GetBytes(postData);

        // Set the ContentType property of the WebRequest.
        request.ContentType = "application/x-www-form-urlencoded";

        // ** Commenting out the bit that writes the post data to the request stream **

        //// Set the ContentLength property of the WebRequest.
        //request.ContentLength = byteArray.Length;
        //// Get the request stream.
        //Stream dataStream = request.GetRequestStream();
        //// Write the data to the request stream.
        //dataStream.Write(byteArray, 0, byteArray.Length);
        //// Close the Stream object.
        //dataStream.Close();

        // Get the response.
        WebResponse response = request.GetResponse();
        // Display the status.
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        // Get the stream containing content returned by the server.
        Stream dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        // Display the content.
        Console.WriteLine(responseFromServer);
        Console.WriteLine(i);
        // Clean up the streams.
        reader.Close();
        dataStream.Close();
        response.Close();

您能否建议我在我的代码中进行哪些更改,以便每个请求可以将输入文本限制增加到 40k - 50k。

【问题讨论】:

    标签: c# google-translate


    【解决方案1】:

    在某些时候,有人将您的代码从发出 POST 请求更改为发出 GET。

    GET 将所有数据放入 URL 中,而不是将其放入请求正文中。 URL 有长度限制。回到使用 POST,这个问题就会消失。请参阅您的 HTTP 客户端库的文档以了解如何执行此操作。

    【讨论】:

    • HI SLIM 我已经搜索了所有内容,但无法获取 POST 请求的代码。能不能帮我写一下。这将是一个很大的帮助苗条。
    • 我不明白。我用谷歌搜索了“WebRequest POST”并获得了几十个合适的点击。在继续使用翻译之前,您可能需要确保了解您的 HTTP 库。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-26
    • 2016-08-09
    • 2018-12-13
    • 1970-01-01
    • 2014-04-16
    相关资源
    最近更新 更多