我必须从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服务器或在我的代码。任何人都可以帮助我?
我忘了说,leerByteFichero函数只是读一个文件并将其转换为一个字节[] – Ainur 2013-03-22 08:57:48