java - 时间戳响应错误

标签 java http http-headers timestamp

我必须从 tsa 服务器获取时间戳。我正在发送一个文件(以 byte[] 格式转换)。
但是当我尝试获取响应时,它会抛出一个 NullPointerException

这是我的代码:

public static void timeStampServer () throws IOException{
        //String TSA_URL1    = "http://tsa.starfieldtech.com/";
        String TSA_URL2 = "http://ca.signfiles.com/TSAServer.aspx";
        //String TSA_URL3 = "http://timestamping.edelweb.fr/service/tsp";
        try {
            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_URL2);
            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());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

我正在尝试使用 3 个 tsa 服务器,但其中任何一个都返回给我有效的 TSA。
所有这些都会在
TimeStampResponse response = new TimeStampResponse(resp);”中引发 NullPointerException。

TSA_URL2 引发

java.io.IOException: Received HTTP error: 411 - Length Required.

我不知道问题是出在 tsa 服务器还是我的代码中。有人可以帮助我吗?

最佳答案

我所看到的问题在于您的请求(NullPointer 来自空响应,因为您没有得到响应)。具体来说,问题是 HTTP 请求 header 后面没有冒号。这使得服务器无法读取强制性的内容长度 header 。来自 RFC2616第 4.2 节(HTTP 文档):

HTTP header fields, which include general-header (section 4.5), request-header (section 5.3), response-header (section 6.2), and entity-header (section 7.1) fields, follow the same generic format as that given in Section 3.1 of RFC 822. Each header field consists of a name followed by a colon (":") and the field value.

TL;博士:

更改:

        con.setRequestProperty("Content-type", "application/timestamp-query");
        con.setRequestProperty("Content-length", String.valueOf(request.length));

至:

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

关于java - 时间戳响应错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15566103/

相关文章:

c# - 如何让 StreamReader.ReadLine() 区分 "\r\n"和 "\n"?

http - Web 服务器容忍高客户端轮询率 : Cowboy vs. Yaws Web 服务器

http - 为什么服务器 HTTP header 存在?

java - Jersey 客户端解码 JSON 包含根元素错误

java - 不切片返回抽象类的实例

java - 是否可以使用 view inflator 给单个元素充气?

java - 将客户端 REQUEST_ENTITY_PROCESSING 设置为 CHUNKED 我丢失了文件

java - HTTPS 主机名错误 : should be <xxx. xxx.xxx.xxx>,检查 URLSpoofing。传递的主机名是什么?

angular - 如何使用http拦截器拦截angular-file-upload模块文件上传请求

ruby-on-rails - 如何在 Ruby 中只读取 URL 的 header ?