android - httpsurlconnection 'get' 405 - method not allowed
Asked Answered
E

1

6

I am using a 'get' method on HttpsUrlConnection. When i am testing my code on eclipse (Windows) it works fine. When i compress it to a jar file and use it on android studio it gives me '405 - method not allowed'. What's mean, I am running the method with bad verb (expected GET).

This is how i set the http method type:

        conn.setRequestMethod("GET");

I am setting the http method to 'get' and when i debug it - conn.getRequestMethod = GET and conn.delegate.getRequestMethod = POST.

The error response is - {"Message":"The requested resource does not support http method 'POST'."}

delegate method type

main conn method type

EDITED - code added:

 public static HttpsURLConnection getClient(URL url, LoginToken securityToken, RequestMethod methodType, String requestFormat, String responseFormat) throws IOException, SecurityTokenException {
    HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
    conn.addRequestProperty("Accept-Charset", "UTF-8");
    conn.addRequestProperty("Content-Type", requestFormat);
    conn.addRequestProperty("Accept", responseFormat);
    conn.addRequestProperty("User-Agent", "***SDK/1.0");
    conn.setRequestProperty("Cache-Control", "no-cache");
    if(securityToken != null) {
        LoginToken.ValidateToken(securityToken);
        conn.addRequestProperty("Authorization", String.format("Bearer %1$s", new Object[]{securityToken.getTemporarySecurityCode()}));
    }

    conn.setRequestMethod(methodType.toString());
    conn.setUseCaches(false);
    conn.setDoOutput(true);
    return conn;
}

  public Boolean IsCompleted() throws SecurityTokenException, CommandFailedException {
    LoginToken.ValidateToken(this.getSecurityToken());
    HttpsURLConnection conn = null;
    Gson gson = (new GsonBuilder()).create();

    String json;
    try {
        URL url = new URL(String.format("https://api.***.com/%1$s/detector/%2$s/status", new Object[]{"v1", this.getPID()}));
        conn = ***Client.getClient(url, this.getSecurityToken(), RequestMethod.GET, "application/json", "text/plain");
        Throwable response1;
        BufferedInputStream inputStream;
        if(conn.getResponseCode() != 200) {
            response1 = null;
            inputStream = null;

            String response2;
            try {
                BufferedInputStream inputStream1 = new BufferedInputStream(conn.getErrorStream());

                try {
                    response2 = HttpURLConnectionHelper.convertStreamToString(inputStream1);
                } finally {
                    if(inputStream1 != null) {
                        inputStream1.close();
                    }

                }
            } catch (Throwable var41) {
                if(response1 == null) {
                    response1 = var41;
                } else if(response1 != var41) {
                    response1.addSuppressed(var41);
                }

                throw response1;
            }

            BadLoginResponse response4 = (BadLoginResponse)gson.fromJson(response2, BadLoginResponse.class);
            if(response4 == null) {
                throw new RuntimeException("Unable to process server response.");
            }

            throw new CommandFailedException(response4.getMessage(), conn.getResponseCode());
        }

        Throwable response = null;
        response1 = null;

        try {
            inputStream = new BufferedInputStream(conn.getInputStream());

            try {
                json = HttpURLConnectionHelper.convertStreamToString(inputStream);
            } finally {
                if(inputStream != null) {
                    inputStream.close();
                }

            }
        } catch (Throwable var43) {
            if(response == null) {
                response = var43;
            } else if(response != var43) {
                response.addSuppressed(var43);
            }

            throw response;
        }
    } catch (IOException var44) {
        throw new RuntimeException(var44.getMessage());
    } finally {
        if(conn != null) {
            conn.disconnect();
        }

    }

How can i fix it?

Erinn answered 31/10, 2015 at 12:12 Comment(6)
it seems that u are sending post request for specific url which is not allowed for postCulbertson
That is the problem. I set the http method to GET and the connection delegate still sends a POST request.Erinn
could u show ur codeCulbertson
Please remove the line conn.setDoOutput(true);Murk
thanks @BNK, thanks alotPronunciation
@aspHunter happy coding :)Murk
E
15

Thanks BNK! The solution was to remove conn.setDoOutput(true)

Erinn answered 4/11, 2015 at 21:3 Comment(1)
Glad it could help :)Murk

© 2022 - 2024 — McMap. All rights reserved.