1

SSL証明書をスキップしてhttpsサーバーに接続している間(つまり、すべてのホストを許可します)。そのようなhttpsサーバーにログインした後、getまたはpostリクエストを起動するために毎回ログインする必要がありますか。

私はアンドロイドでこれを試しています。良いポインタがあれば役に立ちます。

httpclientを使用してhttpsサーバーにログインします(すべてを許可してSSLをスキップします)ログイン後に単純なGetリクエストを起動します。

この単純なシナリオのサンプルコードベースはありますか。

4

1 に答える 1

1

まず、ネットワーク呼び出しはメインスレッドで実行されないため、コードを非同期タスクに配置する必要があります。

次に、次のように使用できます。

private RegistrationInfo AsyncRegisterDevice(
                AndroidDeviceInfo deviceInfo, NetworkIdentification networkId, long NMEC) {
            RegistrationInfo reqResp = new Objects().new RegistrationInfo();

            try {

                JSONStringer deviceRegistration = new JSONStringer().object()
                        .key("DeviceInfo").object().key("androidId")
                        .value(deviceInfo.androidId).key("imei")
                        .value(deviceInfo.imei).key("mac")
                        .value(deviceInfo.mac).key("brand")
                        .value(deviceInfo.brand).key("product")
                        .value(deviceInfo.product).key("model")
                        .value(deviceInfo.model).key("manufacturer")
                        .value(deviceInfo.manufacturer).key("device")
                        .value(deviceInfo.device).key("serial")
                        .value(deviceInfo.serial).key("carrierNumber")
                        .value(deviceInfo.carrierNumber).endObject()
                        .key("UserIdentification").object().key("userName")
                        .value(networkId.username).key("password")
                        .value(networkId.password).endObject()
                        .key("nmec").value(NMEC).endObject();

                HttpPost request = new HttpPost(hostProtocol + "://"
                        + hostAddress + "/Services/Register.svc/Register");
                request.setHeader("Accept", "application/json");
                request.setHeader("Content-Type", "application/json");

                StringEntity requestEntity = new StringEntity(
                        deviceRegistration.toString());

                request.setEntity(requestEntity);

                DefaultHttpClient httpClient = (DefaultHttpClient) CSRHttpClient
                        .getNewHttpClient();

                String message = new String();
                HttpEntity responseEntity = null;

                try {
                    HttpResponse httpResponse = httpClient.execute(request);
                    responseEntity = httpResponse.getEntity();
                } catch (Exception ex) {
                    message = ex.getMessage();
                    android.util.Log.e("CSR", message);
                    return new Objects().new RegistrationInfo();
                }

                if (responseEntity == null)
                    return reqResp;

                char[] buffer = new char[(int) responseEntity
                        .getContentLength()];
                InputStream stream = responseEntity.getContent();
                InputStreamReader reader = new InputStreamReader(stream);
                reader.read(buffer);
                stream.close();

                JSONObject jsonRegInfo = new JSONObject(new String(buffer));

                long androidId = jsonRegInfo.getLong("androidRegistrationId");
                long userId = jsonRegInfo.getLong("userRegistrationId");
                String token = jsonRegInfo.get("registrationToken").toString();

                reqResp.androidRegistrationId = androidId;
                reqResp.registrationToken = token;
                reqResp.userRegistrationId = userId;

            } catch (JSONException jsonEx) {
                String message = jsonEx.getMessage();
            }

            catch (NullPointerException n) {
                String message = n.getMessage();
            } catch (Exception ex) {
                String message = ex.getMessage();
            }
            return reqResp;
        }
    }

このコードは、WCF Webサービスに対してJSon要求を行い、JSon応答を取得します。この応答は、最終的に特定のオブジェクトに対して解析され、その後返されます。

public class CSRHttpClient {

    public static HttpClient getNewHttpClient()
    {
        try
        {
            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            trustStore.load(null, null);

            SSLSocketFactory sf = new CSRSSLSocketFactory(trustStore);
            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

            HttpParams params = new BasicHttpParams();
            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

            SchemeRegistry registry = new SchemeRegistry();
            registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
            registry.register(new Scheme("https", sf, 443));

            ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

            return new DefaultHttpClient(ccm, params);
        } catch (Exception ex)
        {
            return new DefaultHttpClient();
        }

    }


}

このクラスは、すべての有効および無効なサーバー証明書を受け入れることができるカスタムソケットファクトリをインスタンス化するためにのみ機能します。他のいくつかの脆弱性と同様に、すべての証明書を有効なものとして受け入れると中間者攻撃が可能になるため、機密情報サービス/トランスポートでこのような慣行を行うことはお勧めしません。

これがお役に立てば幸いです。

幸運を祈ります。

于 2012-09-21T17:01:06.933 に答える