java - android - httpsurlconnection 'get' 405 - 方法不允许

标签 java android http https httpsurlconnection

我在 HttpsUrlConnection 上使用“获取”方法。当我在 eclipse (Windows) 上测试我的代码时,它工作正常。当我将它压缩到一个 jar 文件并在 android studio 上使用它时,它会给我“405 - 方法不允许”。什么意思,我正在运行带有错误动词的方法(预期 GET)。

这是我设置 http 方法类型的方式:

        conn.setRequestMethod("GET");

我将 http 方法设置为“get”,当我调试它时 - conn.getRequestMethod = GET 和 conn.delegate.getRequestMethod = POST。

错误响应是 - {"Message":"The requested resource does not support http method 'POST'."

delegate method type

main conn method type

已编辑 - 添加代码:

 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();
        }

    }

我该如何解决?

最佳答案

谢谢 BNK! 解决方案是删除 conn.setDoOutput(true)

关于java - android - httpsurlconnection 'get' 405 - 方法不允许,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33451431/

相关文章:

android - 无法为 com.android.build.gradle.internal.dsl.BaseAppModuleExtension 类型的对象设置未知属性 'useAndroidX'

apache - 如何让 apache 始终返回带有数据的代码 200 而不是 304?

c++ - OSX 10.8.2 上的 Poco C++ 库 : Undefined symbols for architecture x86_64

java - 在由 recyclerView 制作的图像 slider 中添加下一个、上一个按钮

java - 如何使用传递的参数等列对oracle select进行排序

java - Java中intern()方法定义的位置?

java - TextView 不显示在对话框中

java - 急切加载 List<Ref<T>> 的所有条目

android - Animation.AnimationListener 和 Animator.AnimatorListener 有什么区别?

java - 从身份验证后的 Web 获取 XML