使用 Jersey 对 Google Translate 进行 POST 调用会返回 HTTP 404
我正在尝试使用 Jersey 1.5 编写对 Google Translate 的 POST 调用。这是我的代码:
package main;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import javax.ws.rs.core.MultivaluedMap;
public class Main {
private static String GOOGLE_TRANSLATE_URL = "https://www.googleapis.com/language/translate/v2";
private static String translateString(String sourceString, String sourceLanguage, String targetLanguage) {
String response;
Client c = Client.create();
WebResource wr = c.resource(GOOGLE_TRANSLATE_URL);
MultivaluedMap<String, String> params = new MultivaluedMapImpl();
params.add("q", sourceString);
params.add("source", sourceLanguage);
params.add("target", targetLanguage);
params.add("key", "xxxx");
wr.header("X-HTTP-Method-Override", "GET");
response = wr.post(String.class, params);
return response;
}
public static void main(String[] args) {
System.out.println(translateString("Hello", "en", "sv"));
}
}
当我运行此代码时,我得到的只是: com.sun.jersey.api.client.UniformInterfaceException: POST https://www.googleapis.com/language/translate/v2 返回了 404 的响应状态
。
我已经成功地使用一个简单的 cURL 命令来完成此操作,如下所示:
curl --header "X-HTTP-Method-Override: GET" -d key=xxxx -dq=Hello -d source=en -d target =sv https://www.googleapis.com/language/translate/v2
提前致谢!
I'm trying to write a POST call to Google Translate with Jersey 1.5. This is my code:
package main;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import javax.ws.rs.core.MultivaluedMap;
public class Main {
private static String GOOGLE_TRANSLATE_URL = "https://www.googleapis.com/language/translate/v2";
private static String translateString(String sourceString, String sourceLanguage, String targetLanguage) {
String response;
Client c = Client.create();
WebResource wr = c.resource(GOOGLE_TRANSLATE_URL);
MultivaluedMap<String, String> params = new MultivaluedMapImpl();
params.add("q", sourceString);
params.add("source", sourceLanguage);
params.add("target", targetLanguage);
params.add("key", "xxxx");
wr.header("X-HTTP-Method-Override", "GET");
response = wr.post(String.class, params);
return response;
}
public static void main(String[] args) {
System.out.println(translateString("Hello", "en", "sv"));
}
}
When I run this, all I get back is this: com.sun.jersey.api.client.UniformInterfaceException: POST https://www.googleapis.com/language/translate/v2 returned a response status of 404
.
I've managed to accomplish this with a simple cURL command like so:
curl --header "X-HTTP-Method-Override: GET" -d key=xxxx -d q=Hello -d source=en -d target=sv https://www.googleapis.com/language/translate/v2
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我怀疑普通 HTTP 服务器不会接受内容长度为零的 POST。 RFC 没有定义这种情况,但 POST 的主要假设是您要发送消息正文。
查看 Google API,他们提到了以下内容
这意味着您不需要在 URL 中添加 q、source 和 target 参数,而是需要在 POST 正文中添加。我对 Jersey API 不熟悉,简单看一下,您只需将 params 作为显式第二个参数添加到 .post 调用中,删除 queryParams() 调用,然后正确设置 Content-Length 即可。
I suspect that POST with zero Content-Length is not something a normal HTTP server will accept. The RFC does not define this case, but the main assumption of POST is that you're sending a message body.
Looking at the Google API, they mention the following
This means that instead of adding q, source and target parameters in the URL, you need to do so in the POST body. I'm not familiar with the Jersey API, from a brief look you just need to add params as an explicit second parameter to the .post call, remove the queryParams() call, and set the Content-Length properly.
我认为最好的、正确的方法是这样的
I think the best and correct way is this
我能够像这样发送很长的文本!
客户端:
服务器:
I was able to send very long text like this!
Client:
Server:
我切换到 Apache HttpClient 4.x 并像这样解决它:
真的不知道为什么它比 Jersey 效果更好,但它确实有效。感谢您尝试提供帮助!
I switched to Apache HttpClient 4.x and solved it like this instead:
Don't really know why this works better than Jersey, but it works. Thanks for trying to help!