带有自签名证书的Android HttpsURLConnection

时间:2014-08-08 08:39:19

标签: java android certificate httpsurlconnection

我正在使用HttpsURLConnection通过GET方法获取一些信息。我在Java中使用基本身份验证的代码和像这样的Trustmanager(HTTPS GET (SSL) with Android and self-signed server certificate

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

可以使用服务器

0 个答案:

没有答案
相关问题