gpt4 book ai didi

java - 时间戳响应错误

转载 作者:行者123 更新时间:2023-12-01 14:44:35 25 4
gpt4 key购买 nike

我必须从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com