【问题标题】:OkHttpClient in android is giving 405 Request Method Not Supported error?android 中的 OkHttpClient 给出 405 Request Method Not Supported 错误?
【发布时间】:2019-11-06 17:06:46
【问题描述】:

我想将 IoT 数据从 android 应用程序发送到 SAP Cloud Platform IoT 服务。为此,我正在使用 OkhttpClient。 用于发送请求的代码是

 private String doGetAsString()
        throws IOException {

    if (connection == null) {
        connect(serverUri);
    }

    connection.setRequestMethod("GET");
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setUseCaches(false);
    connection.setRequestProperty("Content-Type", "application/json");

    try {
        Response response = connect(connection);
        String body = response.getBody();

        Console.printText(String.format("Response [%1$d] %2$s", response.getCode(), body));

        return body;
    }
    finally {
        disconnect();
    }
}

从上述代码中获取响应的代码是

private Response connect(HttpURLConnection connection)
            throws IOException {

        try {
            connection.connect();
        }
        catch (ConnectException e) {
            String errorMessage = "Unable to connect. Please check your Internet connection and proxy settings.";
            throw new IOException(errorMessage, e);
        }

        int code = connection.getResponseCode();

        InputStream stream;
        if (code < HttpURLConnection.HTTP_OK || code >= HttpURLConnection.HTTP_MULT_CHOICE) {
            stream = connection.getErrorStream();
        }
        else {
            stream = connection.getInputStream();
        }

        String body = null;
        try {
            if (stream == null) {
                body = connection.getResponseMessage();
            }
            else {
                body = readString(stream);
            }
        }
        finally {
            FileUtil.closeStream(stream);
        }

        return new Response(code, body);
    }

connection的值为com.android.okhttp.internal.huc.HttpURLConnectionImpl:https://c432c5b0-3ec2-47ad-bf22-23645fa5e565.eu10.cp.iot.sap/c432c5b0-3ec2-47ad-bf22-23645fa5e565/iot/core/api/v1/tenant/727792470/gateways?filter=protocolId%20eq%20'rest'%20and%20status%20eq%20'online'%20and%20type%20eq%20'cloud'

传递给请求的 serverUri 是https://c432c5b0-3ec2-47ad-bf22-23645fa5e565.eu10.cp.iot.sap/c432c5b0-3ec2-47ad-bf22-23645fa5e565/iot/core/api/v1/tenant/727792470/gateways?filter=protocolId eq 'rest' and status eq 'online' and type eq 'cloud'

但是从客户端发送的 uri 看起来像 https://c432c5b0-3ec2-47ad-bf22-23645fa5e565.eu10.cp.iot.sap/c432c5b0-3ec2-47ad-bf22-23645fa5e565/iot/core/api/v1/tenant/727792470/gateways?filter=protocolId%20eq%20'rest'%20and%20status%20eq%20'online'%20and%20type%20eq%20'cloud'

在浏览器中处理以下网址时,我得到了预期的结果 https://c432c5b0-3ec2-47ad-bf22-23645fa5e565.eu10.cp.iot.sap/c432c5b0-3ec2-47ad-bf22-23645fa5e565/iot/core/api/v1/tenant/727792470/gateways?filter=protocolId%20eq%20%27rest%27%20and%20status%20eq%20%27online%27%20and%20type%20eq%20%27cloud%27

通过用户名和密码进行身份验证

 public void connect(String serverUri)
        throws IOException {
    this.serverUri = serverUri;

    connection = openConnection(serverUri);

    if (user != null && password != null) {
        //TODO 2
        byte[] encodedBytes = android.util.Base64.encode((user + ":" + password).getBytes(), Base64.DEFAULT);
        String base64 = new String(encodedBytes, Constants.DEFAULT_ENCODING);
        connection.setRequestProperty("Authorization", "Basic " + base64);
    }
    else if (sslSocketFactory != null && connection instanceof HttpsURLConnection) {
        ((HttpsURLConnection) connection).setSSLSocketFactory(sslSocketFactory);
    }
    else {
        throw new IOException("No authorization details provided");
    }
}

【问题讨论】:

  • 这个错误是关于Http Method的,确定serverUri接受GET吗?可以使用 Postman 重现此调用吗?
  • @JonathasNascimento 是的,我可以在 Postman 中重现 GET 请求。

标签: java android okhttp http-status-code-405


【解决方案1】:

基本上,当您的服务器不支持 GET 并且您正在使用它时,就会发生这种情况。但如前所述,您在浏览器中获取它 GET 将正常工作。

我注意到您使用的是 HTTPS Uri,您是否允许在您的应用中使用 SSL 证书。 否则请检查以下链接以添加 SSL。

Does OkHttp support accepting self-signed SSL certs?

【讨论】:

  • 我正在使用用户名和密码进行身份验证。
猜你喜欢
  • 1970-01-01
  • 2022-11-22
  • 2013-10-10
  • 2020-02-16
  • 2012-07-25
  • 1970-01-01
  • 2021-04-27
  • 1970-01-01
  • 2021-12-12
相关资源
最近更新 更多