Closed GoogleCodeExporter closed 9 years ago
Thanks, I've been able to reproduce it, but I can't see what's wrong either yet!
Original comment by rich.mid...@gmail.com
on 24 Sep 2009 at 12:11
Could it be that Google is checking a comination of referrer, user-agent, etc.?
I tried setting the referrer to
something different, but that didn't help. Also,
http://www.java-forum.org/netzwerkprogrammierung/8939-
google-liefert-http-response-code-403-a.html didn't help.
Original comment by kaspar.f...@gmail.com
on 24 Sep 2009 at 1:17
you do a POST with the method GoogleAPI.retrieveJSON(url). Or google Detect only
responde to a GET method.
/**
* Forms an HTTP request and returns the result of the request as a JSONObject.
*
* @param url The URL to query for a JSONObject.
* @return The translated String.
* @throws Exception on error.
*/
protected static JSONObject retrieveJSON(final URL url) throws Exception {
try {
final HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.setRequestProperty("referer", referrer);
try {
final String result = inputStreamToString(uc.getInputStream());
return new JSONObject(result);
} finally { // http://java.sun.com/j2se/1.5.0/docs/guide/net/http-keepalive.html
uc.getInputStream().close();
if (uc.getErrorStream() != null) {
uc.getErrorStream().close();
}
}
} catch (Exception ex) {
throw new Exception("[google-api-translate-java] Error retrieving
translation.", ex);
}
}
Adn it works perfectly
Original comment by samuel.s...@gmail.com
on 30 Sep 2009 at 12:13
You're right. Adding
uc.setRequestMethod("GET");
to method retrieveJSON() does the trick. At least for this particular test.
Original comment by kaspar.f...@gmail.com
on 30 Sep 2009 at 7:56
Really? That didn't work for me and the default HttpURLConnection request
method is
GET anyway?
Original comment by rich.mid...@gmail.com
on 1 Oct 2009 at 8:21
I am using the transalate jar and specifically using the Detect api. It throws
a 405
error. Can u update the thread
Original comment by shaheen....@gmail.com
on 12 Oct 2009 at 11:37
I'm also experiencing this problem? Has anyone come up with a solution?
Original comment by fup...@gmail.com
on 30 Oct 2009 at 3:20
Ok, I've got it working but I'm not 100% sure why. If you remove
final PrintWriter pw = new PrintWriter(uc.getOutputStream());
in receiveJSON() (in GoogleApi.java) the exception isn't thrown.
Maybe someone can explain what is going on. Does creating the printwriter and
calling
getOutputStream edit the header of the http request into somthing google
doesn't like?
Original comment by fup...@gmail.com
on 30 Oct 2009 at 3:44
Google detection API only works with GET method.
As soon as you ask for an OutputStream, the Java API switches the request
method to
POST. Removing that particular line of code leaves the Java API in the default
GET
method.
I attach a patch against current svn trunk to address the issue. I've added the
explicit setRequestMethod("GET") just for clarity.
This patch together with my previous patch in issue #77 makes all tests green
again.
Original comment by kramar.tomas
on 31 Oct 2009 at 11:13
Attachments:
Thanks for the fix guys. Fixed in SVN, will be in the next release.
Original comment by rich.mid...@gmail.com
on 2 Nov 2009 at 8:04
Original comment by rich.mid...@gmail.com
on 2 Nov 2009 at 8:07
Original issue reported on code.google.com by
kaspar.f...@gmail.com
on 24 Sep 2009 at 10:58