Android中的OkHttpClient给出405请求方法不受支持的错误?

时间:2019-06-24 21:35:36

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

我想将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);
    }

连接的值为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");
    }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

基本上,当您的服务器不支持GET而您正在使用GET时,就会发生这种情况。但是如前所述,您可以在浏览器中获取GET。

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

Does OkHttp support accepting self-signed SSL certs?