1

我正在使用 HttpsURLConnection 通过 GET 方法获取一些信息。我有使用基本身份验证和像这样的信任管理器的 Java 工作代码(带有 Android 和自签名服务器证书的 HTTPS GET (SSL)

private String sendHTTPrequest(String uri, HttpRequestMethods method,
        String user, String password, int timeout, String body)
        throws Exception {
    if (sxtm != null) {
        URL url = new URL(uri);
        HttpsURLConnection httpsRequest = (HttpsURLConnection) url
                .openConnection();
        httpsRequest.setSSLSocketFactory(sxtm.getSSLInstance()
                .getSocketFactory());
        httpsRequest.setHostnameVerifier(sxtm.getHostInstance());
        httpsRequest.setRequestMethod(""+method);
        httpsRequest.setConnectTimeout(timeout);
        httpsRequest.setDoInput(true);
        httpsRequest.setDoOutput(true);
        if (useDigist) {
            // Currently not in use
            setDigestAuthentication(httpsRequest, user, password);
        } else {
            String authString = user + ":" + password;
            byte[] authEncBytes = Base64Coder.encode(authString
                    .getBytes());
            String authStringEnc = new String(authEncBytes);
            httpsRequest.addRequestProperty("Authorization", "Basic "
                    + authStringEnc);
        }

        System.out.println(httpsRequest.getResponseCode());

        if (httpsRequest.getContentType() != null
                && httpsRequest.getContentType().contains("xml")) {
            DocumentBuilderFactory dfactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder builder = dfactory.newDocumentBuilder();
            lastXmlContent = builder.parse(httpsRequest.getInputStream());
            return httpsRequest.getResponseMessage();
        } else {
            return "No XML found";
        }
    }
    return "TrustManager is null";
}

Base64Coder 是一个自己的类,用于在普通 Java JRE 和 Android 之间切换(这工作,在两个运行时相同的结果)

现在我的问题是在 Java JRE 上一切正常,但在 Android 上我总是得到一个 405 代码!??!?!?

在 Android 应用程序中设置了 Internet 权限,并且可以使用 Firefox 访问服务器

4

0 回答 0