我正在尝试对时间戳服务器进行发布调用,但得到 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");
}
});
我知道标头,凭据和URL都是正确的,但是我不确定我是否将TimeStampRequest正确添加到OkHttp请求中,或者即使MediaType是否是它应该添加的。有人知道它是否正确吗?
注意:TimeStamp 类属于 Spongycastle 库。
事实证明不正确的部分是MediaType
,必须binary
它才能使服务器正确读取数据。代码已更正:
MediaType type = MediaType.parse("binary");