使用google翻译api POST方法,返回404
所以基本上我有一些法语文本,想使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这有点老了,但我刚刚遇到了类似的问题,但使用的是 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.