使用google翻译api POST方法,返回404

发布于 2024-12-01 05:56:01 字数 1731 浏览 3 评论 0原文

所以基本上我有一些法语文本,想使用 c# 将其翻译成英语。

我正在使用谷歌翻译 API,它工作正常,直到我有一个 text.length > 。 1000 ....然后我意识到我必须使用POST方法。

因为我使用 post 方法,所以它返回 404。

顺便说一句,我知道 api 已被弃用,我认为无论如何它都会很酷,但我开始意识到也许我应该使用 bing ?

 string fromLanguage = "fr";
 string toLanguage = "en";
 String apiKey = "AIzasdfasdfJvWKNioZwLg-3kyYsm4_dao";
 String apiUrl = "https://www.googleapis.com/language/translate/v2";
 string tmpTranslatedContent = Translate(apiUrl, "salut la planete", apiKey, fromLanguage, toLanguage);


public string Translate(string url, string text, string key, string fromLanguage, string toLanguage)
        {
            PostSubmitter post = new PostSubmitter();
            post.Url = url;
            post.PostItems.Add("key", key);
            post.PostItems.Add("source", fromLanguage);
            post.PostItems.Add("target", toLanguage);
            post.PostItems.Add("q", text);
            post.Type = PostSubmitter.PostTypeEnum.Post;
            string result = post.Post();
            return result;
       }

搜索 google Comments 时发现的一个类

PostSubmitter 是我在网站上 都说它有效......

课程的主要部分看起来像这样,

HttpWebRequest request=null;
if (m_type==PostTypeEnum.Post)
{
Uri uri = new Uri(url);
request = (HttpWebRequest) WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
using(Stream writeStream = request.GetRequestStream())
{
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes(postData);
writeStream.Write(bytes, 0, bytes.Length);
}

谢谢。

So basically I have some french text and want to traduce it in english using c#.

I'm using google translate api, which was working fine until i had a text.length > 1000 .... then I realized that I must use POST method.

Since I use the post method, it returns me 404.

btw i know the api is deprecated, I though it would be cool anyways but I'm starting to realize maybe i should use bing ?

 string fromLanguage = "fr";
 string toLanguage = "en";
 String apiKey = "AIzasdfasdfJvWKNioZwLg-3kyYsm4_dao";
 String apiUrl = "https://www.googleapis.com/language/translate/v2";
 string tmpTranslatedContent = Translate(apiUrl, "salut la planete", apiKey, fromLanguage, toLanguage);


public string Translate(string url, string text, string key, string fromLanguage, string toLanguage)
        {
            PostSubmitter post = new PostSubmitter();
            post.Url = url;
            post.PostItems.Add("key", key);
            post.PostItems.Add("source", fromLanguage);
            post.PostItems.Add("target", toLanguage);
            post.PostItems.Add("q", text);
            post.Type = PostSubmitter.PostTypeEnum.Post;
            string result = post.Post();
            return result;
       }

PostSubmitter is a class i found when searching google

Comments on the site are saying it works.....

the main part of the class looks like this

HttpWebRequest request=null;
if (m_type==PostTypeEnum.Post)
{
Uri uri = new Uri(url);
request = (HttpWebRequest) WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
using(Stream writeStream = request.GetRequestStream())
{
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes(postData);
writeStream.Write(bytes, 0, bytes.Length);
}

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明月松间行 2024-12-08 05:56:01

这有点老了,但我刚刚遇到了类似的问题,但使用的是 PHP 而不是 C#,修复应该非常相似。

基本上,即使您使用 POST,您仍然需要告诉 Google,从 REST 的角度来看,您实际上正在执行 GET。这可以通过 X-HTTP-Method-Override 标头来完成,将其设置为: X-HTTP-Method-Override: GET

Google 告诉我,作为 ASP.NET MVC 版本 2,有一个方法 HttpHelper.HttpMethodOverride将允许你这样做。

然而,根据 Google Translate API,即使在发布时,文本仍限制为 5k。

This is a little old, but I just ran into a similar problem but with PHP instead of C# and the fix should be quite similar.

Basically, even though you are using POST, you still need to tell Google that from a REST point of view you are actually doing a GET. This can be done with the X-HTTP-Method-Override header, setting it to be: X-HTTP-Method-Override: GET

Google tells me that as ASP.NET MVC, version 2, there is a method HttpHelper.HttpMethodOverride that will allow you to do this.

According to the Google Translate API however, text is still limited to 5k even when posting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文