0

我试图从 tsa 服务器获取时间戳,但出现此错误。我不知道问题出在哪里。

java.lang.IllegalArgumentException: unknown object in 'TimeStampResp' factory : org.bouncycastle.asn1.DERUnknownTag

在这份声明中

TimeStampResp resp = TimeStampResp.getInstance(new ASN1InputStream(in).readObject());

这是我的代码:

    String TSA_URL    = "http://tsa.starfieldtech.com/";
    //String TSA_URL = "http://ca.signfiles.com/TSAServer.aspx";
    //String TSA_URL = "http://timestamping.edelweb.fr/service/tsp";
    try {
        //byte[] digest = calcularMessageDigest(leerByteFichero("C:\\deskSign.txt"));
        byte[] digest = leerByteFichero("C:\\deskSign.txt");

        TimeStampRequestGenerator reqgen = new TimeStampRequestGenerator();
        TimeStampRequest req = reqgen.generate(TSPAlgorithms.SHA1, digest);
        byte request[] = req.getEncoded();

        URL url = new URL(TSA_URL);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();

        con.setDoOutput(true);
        con.setDoInput(true);
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-type:", "application/timestamp-query");
        con.setRequestProperty("Content-length:", String.valueOf(request.length));

        if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
            throw new IOException("Received HTTP error: " + con.getResponseCode() + " - " + con.getResponseMessage());
        }
        InputStream in = con.getInputStream();
        TimeStampResp resp = TimeStampResp.getInstance(new ASN1InputStream(in).readObject());
        TimeStampResponse response = new TimeStampResponse(resp);
        response.validate(req);
        System.out.println(response.getTimeStampToken().getTimeStampInfo().getGenTime());

任何人都可以帮助我吗?

4

1 回答 1

0

你犯了两个错误:

  1. Content-type 和 Content-length 后面没有冒号
  2. 你忘了实际发送请求

这是您的代码的改编版:

TimeStampRequest request;
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/timestamp-query");
con.setRequestProperty("Content-length", String.valueOf(request.getEncoded().length));
Authenticator.setDefault(new Authenticator() { 
con.getOutputStream().write(request.getEncoded());

if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new IOException("Received HTTP error: " + con.getResponseCode() + " - " + con.getResponseMessage());
}
InputStream in = con.getInputStream();

TimeStampResp resp = TimeStampResp.getInstance(new ASN1InputStream(in).readObject());
TimeStampResponse response = new TimeStampResponse(resp);
于 2013-06-26T13:58:18.497 回答