使用 OkHttp 的时间戳请求

Timestamp request with OkHttp

我正在尝试 post 调用时间戳服务器,但得到的是 500。这是我的代码:

        Security.addProvider(new BouncyCastleProvider());

        TimeStampRequestGenerator reqGen = new TimeStampRequestGenerator();
        reqGen.setCertReq(true);

        MessageDigest digest = MessageDigest.getInstance("SHA256");
        digest.update(myData);
        TimeStampRequest request = reqGen.generate(TSPAlgorithms.SHA256, digest.digest(), BigInteger.valueOf(100));

        MediaType type = MediaType.parse("application/ocsp-request");
        RequestBody requestBody = RequestBody.create(type, request.getEncoded());

        Request request = new Request.Builder()
                .url(myUrl)
                .post(requestBody)
                .addHeader("Content-type", "application/timestamp-query")
                .addHeader("Content-Transfer-Encoding", "binary")
                .addHeader("Authorization", Credentials.basic(myUser, myPass))
                .build();

        httpClient.newCall(request).enqueue(new Callback() {
            public void onFailure(Call call, IOException e) {
                Log.e(myTAG, "Error connecting to timestamp server");
            }

            public void onResponse(Call call, Response response) {
                Log.e(myTAG, "Success");
            }
        });

我知道 headers、凭据和 url 都是正确的,但我不确定我是否将 TimeStampRequest 正确添加到 OkHttp 请求,或者即使MediaType 是它应该的那种。有人知道它是否正确吗?

注意:TimeStamp 类 属于 spongycastle 库。

原来不正确的部分是 MediaType,服务器必须是 binary 才能正确读取数据。更正的代码:

MediaType type = MediaType.parse("binary");