rakesh441 / google-api-translate-java

Automatically exported from code.google.com/p/google-api-translate-java
0 stars 0 forks source link

DetectTest fails with server returning HTTP response code 405 #68

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Compile the source (latest from SVN repository)
2. Run the DetectTest JUnit test

What is the expected output? What do you see instead?

The test fails with this trace:

java.lang.Exception: [google-api-translate-java] Error retrieving translation.
    at com.google.api.GoogleAPI.retrieveJSON(GoogleAPI.java:104)
    at com.google.api.detect.Detect.execute(Detect.java:56)
    at test.DetectTest.testDetect(DetectTest.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav
a:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at junit.framework.TestCase.runTest(TestCase.java:168)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:232)
    at junit.framework.TestSuite.run(TestSuite.java:227)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:79)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at 
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner
.java:467)
    at 
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner
.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.io.IOException: Server returned HTTP response code: 405 for 
URL: 
http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=Hello+world
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at 
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorI
mpl.java:39)
    at 
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorA
ccessorImpl.
java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1368)
    at java.security.AccessController.doPrivileged(Native Method)
    at 
sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnectio
n.java:13
62)
    at 
sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.jav
a:1016)
    at com.google.api.GoogleAPI.retrieveJSON(GoogleAPI.java:94)
    ... 21 more
Caused by: java.io.IOException: Server returned HTTP response code: 405 for 
URL: 
http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=Hello+world
    at 
sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.jav
a:1313)
    at com.google.api.GoogleAPI.retrieveJSON(GoogleAPI.java:88)
    ... 21 more

Strangely, if I visit the URL (see stack trace)

  http://ajax.googleapis.com/ajax/services/language/detect?v=1.0&q=Hello+world

the HTTP response is 200, so it looks good.

What version of the product are you using? On what operating system?

Java 5, MacOS X, Eclipse 3.5

Original issue reported on code.google.com by kaspar.f...@gmail.com on 24 Sep 2009 at 10:58

GoogleCodeExporter commented 8 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

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
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:

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago

Original comment by rich.mid...@gmail.com on 2 Nov 2009 at 8:07