如何在Android上接受自签名证书

时间:2015-07-22 07:52:56

标签: android rest certificate self-signed android-webservice

经过几天毫无结果的搜索和尝试各种建议和例子,我仍然在努力解决这个问题。

情况是我们正在开发针对开发系统的Android应用程序(即不能在IT之外使用)。它有一个自签名证书。实时系统将有一个“适当的”证书,所以我真的不想禁用证书检查只是为了开发它。

我收到的错误是401授权错误。我通过fiddler尝试了使用Authorization凭据的请求,并且REST服务很好地消失了。

证书位于用户下设备的受信任凭证存储区中。

以下是建立连接的代码。

try {
         StringBuilder pathName = new StringBuilder();
         StringBuilder data = new StringBuilder();

         pathName.append(getApiCall()+ path);

         URL url = new URL(pathName.toString());

         HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection(Proxy.NO_PROXY);

         urlConnection.setRequestProperty("Autorization", getCredentials());
         urlConnection.setDoInput(true);
         urlConnection.setRequestMethod("GET");
         urlConnection.setRequestProperty("Content-Type", "application/json");

         try
         {
             InputStream in = urlConnection.getInputStream();
             BufferedReader r = new BufferedReader(new InputStreamReader(in));

             while ( r.readLine() != null)
             {
                 String dataLine = r.readLine();
                 data.append(dataLine);
             }
         }
         catch (Exception ex2)
         {
             int freda = urlConnection.getResponseCode();
             String fred = urlConnection.getResponseMessage();

             data.append("ERROR");
         }
         finally
         {
             urlConnection.disconnect();
         }

         return data.toString();

    }
    catch (Exception e)
    {
        return "ERROR";
    } 

如果有人能指出我正确的方向,我将非常感激。

由于

史蒂夫

0 个答案:

没有答案